Replacing the column value in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing the column value in Unix
# 1  
Old 06-22-2011
Replacing the column value in Unix

Hello all,
I have a file with below content.Its basically huge file but listing few of the content

Code:
A0|SAL |bN  |g10          |                        |20100611|G|1
M0|AL |bN  |g10          |                        |20100611|K|2
n0|L |bN  |g10          |                        |20100611|V|4
t0|SALa |bN  |g10          |                        |20100611|Q|6
w0|ALfd |bN  |g10          |                        |20100611|I|0
q0|BAL |bN  |g10          |                        |20100611|I|7
q0|KAL |bN  |g10          |                        |20100611|I|4
s0|MAL |bN  |g10          |                        |20100611|I|2
f10|PAL |bN  |g10          |                        |20100611|I|6
b0|QAL |bN  |g10          |                        |20100611|I|1

The file looks some what like above.
I just need to replace the last 2 columns of the file with O and ZERO.
for example
Code:
A0|SAL |bN  |g10          |                        |20100611|G|1
M0|AL |bN  |g10          |                        |20100611|K|2

change second last column from G,K to O and 1 and 2 to 0 as in ZERO as below
Code:
A0|SAL |bN  |g10          |                        |20100611|O|0
M0|AL |bN  |g10          |                        |20100611|O|0

Please suggest.
# 2  
Old 06-22-2011
Try this,
Code:
 
perl -F'\|' -lane 'BEGIN{$,="|"}$F[7]="O";$F[8]=0;print @F' input

# 3  
Old 06-22-2011
As always,

An awk version:

awk -F"|" '{$NF=0;$(NF-1)="O"}1' OFS="|" input_file
# 4  
Old 06-22-2011
@panyam-
oh yes, i got it..Earlier i tried a bit similar but with some syntax errors..
I appreciate.
Can you please elaborate the above awk command in short, like what is
Code:
awk -F"|" '{$NF=0;$(NF-1)="O"}1' OFS="|" input_file
what is NF
NF-1 and 
OFS

# 5  
Old 06-22-2011
Hello,

Code:
 
NF => last field , $NF => value placed in last field
 
NF - 1 => last but one field $(NF-1) => value placed in last but one field
 
OFS => output field seperator

Regards
Ravi
# 6  
Old 06-22-2011
Sed solution
Code:
sed 's/\(.*\)|.|.$/\1|0|o/' inputfile > outfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help in replacing column values

Hello All, I am having the file as below .I need to replace column 9-12 with some other values. In the below file I need to replace 1509 to 1508 and 1508 to 1507 .Can you please help me in how to do that Thanks, Arun ... (10 Replies)
Discussion started by: arunkumar_mca
10 Replies

2. Shell Programming and Scripting

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 : 00006139 2066522 DURTS S 0 20121219 20121219 20121120 00006137 2515401 DURTS S 0 20121219 20121219 20121120 00004363 6030777 DURTS S 0 20121219 20121219 ... (2 Replies)
Discussion started by: Ravi Tej
2 Replies

3. Shell Programming and Scripting

replacing column 3 in csv

sample csv 1234,abc,000,def, 5678,abc,010,def, 9012,abc,020,def, desired output 1234,abc,0,def, 5678,abc,1,def, 9012,abc,2,def, I need to change only the 2nd column from 000 to 0, 010 to 1 and 020 to 2 in shell. I tried using awk -F with if condition in column 3. It din help me (2 Replies)
Discussion started by: mumbaiguy07
2 Replies

4. Shell Programming and Scripting

Replacing column values

hi all , ( perl) i am trying to replace jst one column in a file for eg month dayofweek dealar car-name jan thurs mercedes c300 feb wed lexus is300 all this data is in a master file and i want to replace jan with 1 feb... (5 Replies)
Discussion started by: technoman
5 Replies

5. 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

6. 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

7. 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

8. 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

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