How to replace a specific word in specific column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace a specific word in specific column?
# 1  
Old 03-07-2009
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

Smilie
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  
Old 03-07-2009
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  
Old 03-07-2009
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

# 4  
Old 03-07-2009
Quote:
Originally Posted by radoulov
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!Smilie
# 5  
Old 03-07-2009
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  
Old 03-07-2009
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
# 7  
Old 03-07-2009
As I was just palying around with the join command I discovered some neat feature like performing a "left join" type of join:
Code:
join -t' ' -a1 -e'unknown' -12 -21 -o 1.1,1.2,1.3,2.2,1.4,1.5 mainfile reffile

This will return:

Code:
1 host1 ip1 location1 - xxx
2 host2 ip2 location2 - xxx
3 host33 ip3 unknown - xxx
9 host9 ip9 location9 - xxx

Of course, files need to be sorted first which is not the case with awk.

Last edited by ripat; 03-07-2009 at 09:56 AM.. Reason: Sort needed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific column delimiter

Hi All, I have a file with a pipe delimiter. I need to replace the delimiter with html tags. I managed to get all the delimiters replaced along with first and last but the requirement is that I need to change 7th delimiter with slight change. File1: ... (2 Replies)
Discussion started by: shash
2 Replies

2. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

3. Shell Programming and Scripting

Replace a specific column with a specific value

Hi, I am looking to replacing value of a specific column of /etc/pam.d/system-auth file. My file looks like this password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok expected result password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok... (5 Replies)
Discussion started by: Litu1988
5 Replies

4. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

5. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

6. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

8. Shell Programming and Scripting

Help with replace column data with specific word

Input file: Populus_trichocarpa 30 0 50 0 0 US Vitis_vinifera 1 18 2 8 6 US Populus_trichocarpa 1 5 100 0 0 US Arabidopsis_lyrata_subsp._lyrata 0 90 0 0 0 US Glycine_max 0 2 3 0 70 UK Desired output file: Populus_trichocarpa YES NO YES NO NO US Vitis_vinifera YES YES YES YES YES US... (4 Replies)
Discussion started by: perl_beginner
4 Replies

9. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

10. Shell Programming and Scripting

How to cut first line only from a text near a specific column without cutting a word

First I have to say thank you to this community and this forum. You helped me very much builing several useful scripts. Now, I can't get a solution the following problem, I'm stuck somehow. Maybe someone has an idea. In short, I dump a site via lynx and pipe the output in a file. I need to... (7 Replies)
Discussion started by: lowmaster
7 Replies
Login or Register to Ask a Question