Claus7
February 6th, 2009, 02:30 PM
Hello awk gurus,
I would like your help in a topic about awk, which I think that not only is of vital importance, but also if clarified will help a lot of people.
My question has to do with something like this:
awk pattern file1 file2 file3 (...up to 10) > finalfile
Awk can manage up to ten files simultaneously, if I have searched correctly.
Also, it reads file by file until it reaches the final one.
If someone wants to discriminate the first file from a second one, then something like this would suffice:
awk ' NR=FNR { if (FNR==2) x=$1 ; next }
{ if (FNR==2) y=$1 }
{ if (FNR==2) print x+y }' file1 file2 > file3
The NR=FNR condition sais that while the :
FNR The ordinal number of the current record in the current file is equal to
NR The ordinal number of the current record from the start of input then
x=$1 (for file1)
Next it reads file2, takes from the second line the first element, assigns it to y and it prints in file3 the entire file2, except that the second line is substituted by x+y.
So far so good.
How about when we want to do the above procedure to more than two files? How can I control in which file it enters, takes the value from the second line and in the last file I add all these values and print them to the second line?
very nice links are the following:
http://www.unix.com/shell-programming-scripting/70269-gawk-reading-two-files-re-arrange-columns.html
http://oreilly.com/catalog/unixnut3/chapter/ch11.html
http://www.tek-tips.com/viewthread.cfm?qid=1281506&page=1
http://www.issociate.de/board/post/465434/merge_two_files_line_by_line....html
http://groups.google.com/group/comp.lang.awk/msg/e9622cad4b072635
Regards!
I would like your help in a topic about awk, which I think that not only is of vital importance, but also if clarified will help a lot of people.
My question has to do with something like this:
awk pattern file1 file2 file3 (...up to 10) > finalfile
Awk can manage up to ten files simultaneously, if I have searched correctly.
Also, it reads file by file until it reaches the final one.
If someone wants to discriminate the first file from a second one, then something like this would suffice:
awk ' NR=FNR { if (FNR==2) x=$1 ; next }
{ if (FNR==2) y=$1 }
{ if (FNR==2) print x+y }' file1 file2 > file3
The NR=FNR condition sais that while the :
FNR The ordinal number of the current record in the current file is equal to
NR The ordinal number of the current record from the start of input then
x=$1 (for file1)
Next it reads file2, takes from the second line the first element, assigns it to y and it prints in file3 the entire file2, except that the second line is substituted by x+y.
So far so good.
How about when we want to do the above procedure to more than two files? How can I control in which file it enters, takes the value from the second line and in the last file I add all these values and print them to the second line?
very nice links are the following:
http://www.unix.com/shell-programming-scripting/70269-gawk-reading-two-files-re-arrange-columns.html
http://oreilly.com/catalog/unixnut3/chapter/ch11.html
http://www.tek-tips.com/viewthread.cfm?qid=1281506&page=1
http://www.issociate.de/board/post/465434/merge_two_files_line_by_line....html
http://groups.google.com/group/comp.lang.awk/msg/e9622cad4b072635
Regards!