Replacing a column in a file with a value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a column in a file with a value
# 1  
Old 12-21-2012
Lightbulb Replacing a column in a file with a value

Hi ,

Could anyone help me for the below request .

Suppose i have the data like below :

Code:
00006139 2066522       DURTS   S  0 20121219  20121219  20121120   

00006137 2515401       DURTS   S  0 20121219  20121219  20121120

00004363 6030777       DURTS   S  0 20121219  20121219  20121120 
00035389 2017917       DURTS   S  0 20121219  20121219  20121120


Question :

1. I want to replace the whole column 6 with number 0 .

2 . Suppose i have a value stored in a variable like a=3 , How to replace a particular column using variable .


Please suggest your solutions for the above data .
# 2  
Old 12-21-2012
Here is an example, replacing 6th column with value of variable a:-
Code:
awk -v a=3 'NF>1{ $6=a; print; } ' filename

or
Code:
a=3
awk -v A=$a 'NF>1{ $6=A; print; } ' filename

# 3  
Old 12-21-2012
I think this does it in bash:
Code:
$ bash -c 'declare -a flds
while read -a flds
do
 flds[5]=0
 echo "${flds[*]}"
done
' <<!
1 2 3 4 5 6 7 8 9
a b c d e f g h i j
!
1 2 3 4 5 0 7 8 9
a b c d e 0 g h i j
$

An awk guy will be by in a minute to do it in one busy line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a column in a pipe delimited file

Hi, I have a pipe delimited file as below and I need to replace the 2nd column of each line with null values. 1|10/15/2011|fname1|lname1 2|10/15/2012|fname2|lname2 3|10/15/2013|fname3|lname3 Output file: 1||fname1|lname1 2||fname2|lname2 3||fname3|lname3 I tried this ... (2 Replies)
Discussion started by: member2014
2 Replies

2. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

3. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

4. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

Replacing column 1 in one file with values in other file

Please help me with an shell / awk script to achieve following; File-1: ABCDW01 12322 23322 BDADW01 22232 24453 EDFAW00 32232 23422 and so on, notice that the first coloumn is a code and the another file contains the real value of each entry in the first colum above but not in a... (4 Replies)
Discussion started by: digipak
4 Replies

6. Shell Programming and Scripting

Replacing a column in a file

Hi, I want to replace a column in a file with a file that only has one column. Reading another post this is what I'm trying to do, The existing file has n columns and I paste the extra column using the paste command to make it n+1 paste file1 file2 > file3 Firstly I want to now remove... (3 Replies)
Discussion started by: lost.identity
3 Replies

7. Shell Programming and Scripting

Replacing column with another column of different file by awk

Hi All, I will really appreciate if you kindly lookinto my requirement below and provide me a solution First file format test1.txt qq ww rr tt ee ff qq ww rr tt ee ff Second file format text2.txt aa aa Now o/p I want as text1.txt's 4th column replaced... (5 Replies)
Discussion started by: Pratik4891
5 Replies

8. UNIX for Dummies Questions & Answers

Replacing a column in a text file

Say I had a text file that contained four columns, like the following: Mack Christopher:237 Avondale Blvd:970-791-6419:S Ben Macdonor:30 Dragon Rd:647-288-6395:B I'm making a loop that will replace the fourth column a line in the file with the contents of a variable 'access', but I have no... (6 Replies)
Discussion started by: Sotau
6 Replies

9. UNIX for Advanced & Expert Users

Replacing column with column of another file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8 spaces>> ... (1 Reply)
Discussion started by: manneni prakash
1 Replies

10. UNIX for Dummies Questions & Answers

Replacing column with column of another file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8 spaces>> ... (1 Reply)
Discussion started by: manneni prakash
1 Replies
Login or Register to Ask a Question