![]() |
|
|
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 |
| how to access values of awk/nawk variables outside the awk/nawk block? | saniya | Shell Programming and Scripting | 5 | 05-13-2008 08:37 AM |
| Sed,nawk for inputfile and out put file same | svenkatareddy | Shell Programming and Scripting | 2 | 04-09-2008 08:12 AM |
| I need help counting the fields and field separators using Nawk | scrappycc | Shell Programming and Scripting | 3 | 02-06-2008 11:47 PM |
| Append tabs at the end of each line in NAWK -- varying fields | madhunk | Shell Programming and Scripting | 6 | 07-12-2006 07:20 PM |
| Append a field to the end of each line of a file based on searching another file. | ultimate | Shell Programming and Scripting | 2 | 03-29-2005 11:21 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
|
|
||||
|
append field to file(nawk)
I have two files. File A and File B.
File A has two fields separated by comma 352020252365988, 652020100572356 546876543215667, 652065465654686 ... File B has many Fields separate by spaces Date Name 352020252365988 Reference Date2 Name2 546876543215667 Reference I want to search for a match of Field 1 of file A in file 2. If there is a match print the line that contains the match, and append field 2 of File A at the end of each matched line. so to have Date |
|
|||||
|
If the match is on the 3rd field of the 2nd file, then you can try this awk... Code:
BEGIN {
FS=","
while(getline<xref) arr[$1]=$2
FS=" "
}
$3 in arr {
print $0, arr[$3]
}
Use it like this... awk -f above.awk -v xref=file_a file_b Tested on the sample data... Date Name 352020252365988 Reference 652020100572356 Date2 Name2 546876543215667 Reference 652065465654686 |
|
|||||
|
Just FYI, if you change the code I posted thusly Code:
#!/bin/sh
while read line
do
lookup=`echo "$line" | awk -vFS=',' '{print $1}'`
field=`echo "$line" | awk -vFS=',' '{print $2}' | sed 's/^ //'`
match=`grep "\<$lookup\>" ${2}`
if [ "$?" -eq "0" ]; then
echo "$match" | while read record
do
echo "$record $field"
done
fi
done < ${1}
You can call this with ./my_script file_a file_b - i.e. specifying the files at the prompt as you wish! Cheers ZB |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|