![]() |
|
|
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 |
| Replace if regex on specific column matches expression? | EXT3FSCK | Shell Programming and Scripting | 5 | 11-25-2008 02:00 PM |
| 4 column tsv file, output 1 specific column | casphar | Shell Programming and Scripting | 6 | 11-12-2008 04:04 PM |
| cut a specific value from a column | sfaqih | UNIX for Advanced & Expert Users | 2 | 10-25-2008 07:08 AM |
| how do i parse by specific column? | kmaq7621 | Shell Programming and Scripting | 2 | 09-02-2008 02:01 PM |
| grep a word from a specific line | blurboy | Shell Programming and Scripting | 3 | 01-23-2008 04:13 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
|||||
|
Hi, hope this could help you.. file1: id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx file2: host1 location1 host2 location2 host3 location3 -- -- host9 location9 script: Code:
for f in `sed '1d' file1|awk '{print $2}'`
do
echo $f
value=`awk -v var=$f '{if($1~var) {print $2}}' file2`
if [ $? -eq 0 ]
then
awk -v var=$value -v var1=$f '{if($0~var1){print$1,$2,$3,var,$5}}' file1 |head -1 >> out.lst
fi
done
head -1 file1 > outnew.lst
cat out.lst >> outnew.lst
output: id host ip location remarks 1 host1 ip1 location1 xxx 2 host2 ip2 location2 xxx 3 host3 ip3 location3 xxx 9 host9 ip9 location9 xxx Thanks Sha |
|
|||||
|
Or: (use nawk or /usr/xpg4/bin/awk on Solaris) Code:
awk -F'\t' 'NR == FNR { ref[$1] = $2; next }
FNR == 1 || $4 = ref[$2] ? ref[$2] : "-"
' OFS='\t' reffile mainfile
|
| Bits Awarded / Charged to radoulov for this Post | |||
| Date | User | Comment | Amount |
| 05-22-2009 | ./hari.sh | N/A | 1,000 |
|
|||||
|
Actually it could be less verbose and the previous code won't work if you have a location named 0 or "" (null string), this one should handle those cases too: Code:
awk -F'\t' 'NR == FNR { ref[$1] = $2; next }
($2 in ref && $4 = ref[$2]) || 1
' OFS='\t' reffile mainfile
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|