![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| awk reading 2 input files but not getting expected value | pdtak | Shell Programming and Scripting | 1 | 04-29-2008 10:03 PM |
| Help reading an input file in KSH | zilla30066 | Shell Programming and Scripting | 2 | 02-01-2007 06:45 AM |
| Script for reading an input file | gzs553 | Shell Programming and Scripting | 1 | 10-17-2006 06:55 AM |
| Reading Input from File and Duplicates Output | noelcantona | Shell Programming and Scripting | 6 | 10-18-2005 04:59 AM |
| Reading specific contents from a file and appending it to another file | dnicky | Shell Programming and Scripting | 5 | 10-04-2005 05:45 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Reading specific contents from 1 input files and appending it to another input file
Hi guys,
I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it in the Input file 1 (see output file below highlighted in blue). The content of first input file Input file 1 will keep changing though. If we get a match with first number [before first dot (.)] then concatenate the name from second file at the end with a comma (,). Input file 1 Aug 14 10:37 123.XYZ.106.20061023160210.in Aug 14 12:59 135.XYZ.107.20061023160210.in Aug 14 13:15 234.XYZ.108.20061023160210.in Input file 2 111,ABCD 123,EFGH 124,TYUI 125,RTYUGH 135,RTYT 139,REFG 234,GHJK 345,GHJK 346,JKHG Output file Aug 14 10:37 123.XYZ.106.20061023160210.in, EFGH Aug 14 12:59 135.XYZ.107.20061023160210.in, RTYT Aug 14 13:15 234.XYZ.108.20061023160210.in, GHJK Appreciate your responses. |
|
||||
|
Franklin52 - Thanks for your help. One more question though, If I have another input file
Input file 3 XYZ,Desc1 VZG,Desc2 And if I would like to add more to output file by looking into input file 3, then how would I get the "XYZ" to get the description from input file 3. Output file Desc1,Aug 14 10:37 123.XYZ.106.20061023160210.in, EFGH Desc1,Aug 14 12:59 135.XYZ.107.20061023160210.in, RTYT Desc1,Aug 14 13:15 234.XYZ.108.20061023160210.in, GHJK I tried this but no luck awk -F",| |\\\.\." 'NR==FNR{a[$1]=$2;next} $4 in a{print $0 "," a[$4]} ' file3 file1 Appreciate your responses. Last edited by sksahu; 01-13-2009 at 03:48 PM.. |
|
||||
|
No it didn't work. Let me tell you what is the scenario...
I have 3 input files (example given below) Input file 2 & 3 are a standard file (it will not change) and we have to get the name (second column after comma) from it and append it in the Input file 1 (see output file below highlighted in blue & magenta). The content of first input file Input file 1 will keep changing though. If we get a match with first number [before first dot (.)] then concatenate the name from second file at the start with a comma (,). If we get a match with first string [after first dot (.)] then concatenate the name from third file at the start with a comma (,). Input file 1 123.XYZ.106.20061023160210.in 135.XYZ.107.20061023160210.in 234.XYZ.108.20061023160210.in Input file 2 111,ABCD 123,EFGH 124,TYUI 125,RTYUGH 135,RTYT 139,REFG 234,GHJK 345,GHJK 346,JKHG Input file 3 XYZ,Desc1 VZG,Desc2 Output file EFGH,Desc1,123.XYZ.106.20061023160210.in RTYT,Desc1,135.XYZ.107.20061023160210.in GHJK,Desc1,234.XYZ.108.20061023160210.in I got the first part as following :- awk -F",| |\\\." 'NR==FNR{a[$1]=$2;next} $1 in a{print a[$1] "," $0} ' file2 file1 >> output file But I am not getting the second part. Appreciate you response. Last edited by sksahu; 01-13-2009 at 05:34 PM.. |
|
||||
|
Try this:
Code:
awk -F",|\\\." '
FILENAME=="file3" {a[$1]=$2;next}
FILENAME=="file2" {b[$1]=$2;next}
a[$2] && b[$1] {$0=b[$1]"," a[$2]"," $0}
{print}' file3 file2 file1
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| awk, file manipulation, unix |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|