Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing 2 files with different field separators using awk

Tags:

awk

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
like image 954
jrjc Avatar asked Jul 01 '14 17:07

jrjc


People also ask

Can awk use multiple field separators?

As you can see, you can combine more than one delimiter in the AWK field separator to get specific information.

How do I process multiple files in awk?

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.

How do you use a field separator in 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.

What is awk default 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.


2 Answers

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
like image 91
Ed Morton Avatar answered Dec 28 '22 23:12

Ed Morton


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.

like image 29
Etan Reisner Avatar answered Dec 28 '22 22:12

Etan Reisner