Appending different columns of multiple files in awk

Hello All,
I have three input files

cat file1
col1|col2|col3
a|1|A
b|2|B

cat file2
col1|col2|col3
c|3|C

cat file3
col1|col2|col3
d|4|D
e|5|E

i want below output
file4

col1|col2
a|1
3|C
D|4

there is no logic inside it. I am just looking a way to append different fields of different files in awk

there is one error giving psuedo code i could write.

awk -F"|" '{ print $1,$2 }' file1 '{print $2,$3}' file2 '{print $3,$1}' file3

please help me on this..

Where does the D|4 come from?
What happened to b|2|B ?

---------- Post updated at 10:20 AM ---------- Previous update was at 09:52 AM ----------

The format of an AWK command looks as follows:

awk 'pattern {action}' input-file ...

Which means until here everything will go well:

awk -F"|" '{ print $1,$2 }' file1

The rest is not correct, unless all of them are files.

'{print $2,$3}' file2 '{print $3,$1}' file3

That sounds a bit like what's the answer to the ultimate question of Life, The Universe, and Everything.

Try:

awk '
  BEGIN {
    FS=OFS="|"
    print "col1|col2"
  }

  FNR==n+1 {
    print $p1, $p2
  }
' n=1 p1=1 p2=2 file1 p1=2 p2=3 file2 p1=3 p2=2 file3
col1|col2
a|1
3|C
D|4
1 Like

@Aia Thanks for looking into it. As i said there is no logic , i have randomly selected different columns of different files. I know the code i wrote was not correct that is the reason i mentioned pseudo code just to make it understand. Perhaps it created more confusion. :slight_smile:

@Scrutinizer Thanks , this is what i was looking for. So the magic lies in p1=1 p2=2 file1 p1=2 p2=3 file2 p1=3 p2=2 file3 .
I don't know why thanks button is disable!

awk -v answer42='col1|col2' '
  BEGIN {
    FS=OFS="|"
    print answer42
  }

  FNR==universe+1 {
    print $mouse1, $mouse2
  }
' FS=\| OFS=\| universe=1 mouse1=1 mouse2=2 file1 mouse1=2 mouse2=3 file2 mouse1=3 mouse2=2 file3

[/CODE]

Output:

col1|col2
a|1
3|C
D|4
1 Like

Hello looney,

Could you please try following approach too(with function) and let me know if this helps you.

awk -F"|" '
function print_val(var){
  num=split(var, array,",");
  for(i=1;i<=num;i++){
    printf("%s%s",$array,i==num?RS:"|")
}
}
BEGIN{
  print "col1|col2"
}
FNR==1{
  ++file;
  next
}
file==1{
  print_val("1,2");
  nextfile
}
file==2{
  print_val("2,3");
  nextfile
}
file==3{
  print_val("3,2");
  nextfile
}
'   Input_file1  Input_file2  Input_file3

Output will be as follows.

col1|col2
a|1
3|C
D|4

You need to pass number of fields which you want to print to function named eg--> print_val("1,3") and it should do the trick, since you want to read only very first line of each Input_file so I have used nextfile so that it will NOT go further for Input_file reading and will switch to next Input_file, moreover you need NOT to create many variables in case number of Input_file(s) are too much(tested this in GNU awk ).

Thanks,
R. Singh

If there's no logics to it, you can't program it. Programming means cast logics into code and apply repeatedly onto structurally identical but contentswise differing data, like everyday's log files, or multiple customer entries in a DB.
All above proposals convert your input data exactly as given into output exactly as in post#1. Why don't you do it in a one off editor session?

1 Like