Let's say I have 2 files :
$ cat file1
A:10
B:5
C:12
$ cat file2
100 A
50 B
42 C
I'd like to have something like :
A 10 100
B 5 50
C 12 42
I tried this :
awk 'BEGIN{FS=":"}NR==FNR{a[$1]=$2;next}{FS=" ";print $2,a[$2],$1}' file1 file2
Which outputs me that :
100 A
B 5 50
C 12 42
I guess the problem comes from the Field Separator which is set too late for the second file. How can I set different field separator for different files (and not for a single file) ?
Thanks
Edit: a more general case
With file2 and file3 like this :
$ cat file3
A:10 foo
B:5 bar
C:12 baz
How to get :
A 10 foo 100
B 5 bar 50
C 12 baz 42
As you can see, you can combine more than one delimiter in the AWK field separator to get specific information.
Yes, you can read from multiple files at the same time using awk. In order to do that, you should use the getline command to explicitly control input. In particular, you want to use getline with a file so that you're not reading from the file(s) passed in as main arguments to awk.
Just put your desired field separator with the -F option in the AWK command and the column number you want to print segregated as per your mentioned field separator.
The default value of the field separator FS is a string containing a single space, " " . If awk interpreted this value in the usual way, each space character would separate fields, so two spaces in a row would make an empty field between them.
Just set FS between files:
awk '...' FS=":" file1 FS=" " file2
i.e.:
$ awk 'NR==FNR{a[$1]=$2;next}{print $2,a[$2],$1}' FS=":" file1 FS=" " file2
A 10 100
B 5 50
C 12 42
You need to get awk to re-split $0
after you change FS.
You can do that with $0=$0
(for example).
So {FS=" ";$0=$0;...}
in your final block will do what you want.
Though only doing that the first time you need to change FS will likely perform slightly better for large files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With