Replace column values from other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace column values from other file
# 1  
Old 07-25-2014
Replace column values from other file

I have one file as it has the following format

File1

Code:
S No   Site            IP Address
1         Australia    192.168.0.1/26
2         Australia    192.168.0.2/26
3         Australia    192.168.0.3/26

I need awk/sed command to replace the column2 value ( under Site) with some other value ( example UK) and Column3 value ( Under IP address) needs to replaced from other file ( File 2) which has the IP address contents like this

File2

Code:
IP Address
10.100.10.1/25
10.100.10.2/23
10.100.10.3/24

So the new file File3 should be like this

Code:
S No   Site            IP Address
1          UK            10.100.10.1/25
2          UK            10.100.10.2/23 
3          UK            10.100.10.3/24

Moderator's Comments:
Mod Comment Please use code tags when displaying sample input, output, and code segments. Without CODE tags, groups of contiguous spaces and tabs are converted to a single space. With CODE tags, spacing is preserved.

Last edited by Don Cragun; 07-26-2014 at 01:10 AM.. Reason: Add CODE tags.
# 2  
Old 07-26-2014
Is this a homework assignment?

What difference does it make whether or not you use File1? It seem that all you have in File3 is a new header line, a line sequence number, a random string that the script makes up on the fly, and the contents of File2.
# 3  
Old 07-26-2014
Hi Don

Thanks for reply.Yes, we can use the file 1 to have all the replacement values and new file entries.
# 4  
Old 07-26-2014
You did not answer Don Cragun's question on homework assignment, nor did you show any efforts from your side. So all hints I can give you for now is to skip file1 and read file2 in a loop, adding the two desired columns to each line before printing it out.
# 5  
Old 07-28-2014
RudiC/Don
I am very new to this forum ( do not understand the meaning of home work assignment )and shell script. Hence requires your help.
# 6  
Old 07-28-2014
Please be somewhat creative and consult a dictionary for exact translations of homework assignment. If you are a student and your teacher gives you problems to solve off class times, that's homework.
And, as long as you don't show some efforts from your side to tackle the problem, little help can/will be offered from our side.
# 7  
Old 07-30-2014
Thanks all for your help and encouragement. In the below code, it erases all the delimit value ( i.e ^I) between two columns in the file.

awk 'NR==FNR {a[FNR]=$1;next} {if(FNR>1){$3=""a[FNR]}}1' file1 file > file3

So i modified as below but ^I value added in entire file and didnt give the proper input

awk 'NR==FNR {a[FNR]=$1;next} {if(FNR>1){$3=""a[FNR]}}1' OFS="\t" file1 file > file3

So my question is, how do i replace the column values without modifying the exiting delimit value?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a numeric values in a certain column

Hi All, I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7 I tried awk '{gsub("8","7",$35)}1' infile > outfile ----> not working sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Shell Programming and Scripting

Trying to get an awk script to replace values in column

I'm trying to make an awk script to compare values I've set as var1, var2, and var3 earlier in the script to the values in the userinputted column of four text files called Node1.txt, Node2.txt, Node3.txt, and Node4.txt and then replace the values in that userinputted column with either ttt or gcc,... (8 Replies)
Discussion started by: Eric1
8 Replies

3. UNIX for Dummies Questions & Answers

Replace all decimal values in a column

Hi My input file looks String000002 GeneWise CW 48945 49354 . - 0 Pt=PEQU_00004; String000002 LEN NA 52125 52604 0.945751 - . PID=PEQU_00005;lvid_id=PEQ_28708; String000002 LEN CW 52125 52604 . - 0 ... (3 Replies)
Discussion started by: siya@
3 Replies

4. Shell Programming and Scripting

Selective Replace awk column values

Hi, I have the following data: 2860377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEGATIVE" 32340377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEG-DID"... (3 Replies)
Discussion started by: sdohn
3 Replies

5. UNIX for Dummies Questions & Answers

replace a column with values from another file

Dear all, I have a file1.pdb in pdb format and a dat file2 containing values, corresponding to the atoms in the pdb file. these values (file2.dat) need to be in the column instead of the 0.00 (file1) values for each atom in file1.pdb .(the red values must be replaced by the blue ones,in order)... (11 Replies)
Discussion started by: chen.xiao.po
11 Replies

6. Shell Programming and Scripting

sed replace values of column with ordered numbering

Hello, I am trying to write a shell script that will create a gnuplot file. My main problem is that I have a data file with two columns: 1.05929120E+09 5.0000701214792 1.05930096E+09 5.00006386985764 1.05930584E+09 5.00019465404908 1.05931072E+09 5.00031960589719 ... (2 Replies)
Discussion started by: pau
2 Replies

7. UNIX for Dummies Questions & Answers

Replace values in a specified column of a file

Hello, I have a file with four columns and I would like to replace values in the second column only. An arbitrary example is: 100 A 105 B 200 B 205 C 300 C 305 D 400 D 405 E 500 E 505 F I need to replace the second column as shown below: ... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

8. Shell Programming and Scripting

Find and replace duplicate column values in a row

I have file which as 12 columns and values like this 1,2,3,4,5 a,b,c,d,e b,c,a,e,f a,b,e,a,h if you see the first column has duplicate values, I need to identify (print it to console) the duplicate value (which is 'a') and also remove duplicate values like below. I could be in two... (5 Replies)
Discussion started by: nuthalapati
5 Replies

9. Shell Programming and Scripting

replace the column values.

I have the below file ...where some of the column values should replaced with desired values ....below file u can find that 3 column where ever 'AAA' comes should replaced with ' CC ' NOTE : we have to pass the column number ,AAA,CC (modified value) as the parameters to the code. ... (6 Replies)
Discussion started by: charandevu
6 Replies

10. UNIX for Advanced & Expert Users

replace a column values with the first value in column

Hi All, I have a file which has data in following format: "Body_Model","2/1/2007","2/1/2007" "CSCH74","0","61" "CSCS74","0","647" "CSCX74","0","3" "CSYH74","0","299" "CSYS74","0","2514" "CSYX74","0","3" "Body_Model","3/1/2007","3/1/2007" "CSCH74","0","88" "CSCS74","0","489"... (3 Replies)
Discussion started by: sumeet
3 Replies
Login or Register to Ask a Question