The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-07-2009
./hari.sh's Avatar
./hari.sh ./hari.sh is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 44
How to replace a specific word in specific column?

Hi
My orginal file is like (100s of lines)

Code:
id    host   ip   location  remarks
1    host1  ip1   -   xxx
2    host2  ip2   -   xxx
3    host3  ip3   -   xxx
--
--
9    host9  ip9   -   xxx

I have a ref file like

Code:
host1   location1
host2   location2
host3   location3
--
--
host9   location9

Now I want the first file like

Code:
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


Note: The delimiter is "tab". The word is not the last word in any of the line...
Can you suggest me an idea?

Last edited by ./hari.sh; 03-07-2009 at 01:16 AM..
  #2 (permalink)  
Old 03-07-2009
Shahul's Avatar
Shahul Shahul is offline
Registered User
  
 

Join Date: Aug 2008
Location: India
Posts: 175
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
  #3 (permalink)  
Old 03-07-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
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
  #4 (permalink)  
Old 03-07-2009
./hari.sh's Avatar
./hari.sh ./hari.sh is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 44
Quote:
Originally Posted by radoulov View Post
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
Simply Great!
  #5 (permalink)  
Old 03-07-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
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

  #6 (permalink)  
Old 03-07-2009
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 441
A variation with join:

Code:
join -t' ' -11 -22 -o 2.1,2.2,2.3,1.2,2.4,2.5 reffile mainfile

Use appropriate separator (-t option)

Last edited by ripat; 03-07-2009 at 09:19 AM.. Reason: Output formating
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0