![]() |
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 |
| displaying $ in data | ammu | UNIX for Dummies Questions & Answers | 2 | 02-19-2008 03:00 PM |
| searching and displaying most commonly used words | arunsubbhian | UNIX for Dummies Questions & Answers | 2 | 09-10-2007 07:58 PM |
| displaying with ls | vivekshankar | UNIX for Dummies Questions & Answers | 2 | 05-23-2005 03:46 PM |
| vi editor not displaying? | ananthu_m | SCO | 3 | 10-14-2003 12:51 AM |
| Displaying what a command is doing/has done | quantumechanix | UNIX for Dummies Questions & Answers | 1 | 07-02-2003 08:40 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
searching and displaying help
I have two input files like this:
File-1 ----- a1234 abc town f2345 def village t5678 pqr county File-2 ------ 123456 test1 test2 test3 id-a1234 789012 test2 test4 id-t5678 456789 test7 id-b1234 I want to check the lines that match the first field of File-1 in File-2 (where it appears after the "id") and create two output files as follows (based on whether the item is found in File-2 or not): Output-1 (when the first field of File-1 is found at File-2) ------------------------------------------------------- a1234 123456 t5678 789012 Output-2 (when the first field of File-1 is not found in File-2) ---------------------------------------------------------- f2345 Not Found How to do this in a shell script? Any help will be really appreciated. Thanks, Ajay |
|
||||
|
To print the output to the files you can redirect the outputs like this:
Code:
awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found" > "Output-2"
}' File-2 File-1 > Output-1
Code:
$ cat File-1
a1234 abc town
f2345 def village
t5678 pqr county
$
$ cat File-2
123456 test1 test2 test3 id-a1234
789012 test2 test4 id-t5678
456789 test7 id-b1234
$
$ awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found" > "Output-2"
}' File-2 File-1 > Output-1
$
$ cat Output-1
a1234 123456
t5678 789012
$
$ cat Output-2
f2345 not found
$
|
|
||||
|
Thanks for the script.
But, I have issues when using it when the input file-2 has "test-2" instead of "test-2" or "test-3" instead of "test3" (the use of "-" in other columns.) So, I am looking for the script to look only for the field that has the "id-" but must be able to ignore if the "-" presents in any other column. Any suggestions? |
![]() |
| Bookmarks |
| Tags |
| search, string manipulation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|