Help in replacing column values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help in replacing column values
# 1  
Old 04-03-2015
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


Code:
000125701508150222000000568669021900010000004000027000192000176000100000000011220100000000011335
000125701508150222000000570933005900010000004000251007986000176000100000000011220100000025011335
000125701508150222000000616230004900020000005000017001131000195000100000000011220100000000011335
000125701509150222000000626722013900010000007000152001268000227000100000000011220100000018011335
000125701509150226000000003027003900012050010000015000000000247000100000000012370100000000051943
000125701509150226000000075563007900010000005000408001295000195000100000000012370100000011051943
000125701509150226000000133908002400020000002000779001791000127000100000000012370100000000051943
000125701509150226000000137040005300010000010000834005371000247000100000000012370100000000051943
000125701509150226000000549713003000060000000280637000029000023001000000000012370100000033051943
000125701509150226000000581895019900010000006400079008895000219000400000000012370100000000051943
000125701509150226000000583777019900010000012001168010016000249000800000000012370100000000051943
000125701509150226000000603925008500010000002500407000778000140000100000000012370100000000051943
000125701509150227000000201846005000010000000000158000000000000000100000000012370100000000060934


Last edited by Scrutinizer; 04-03-2015 at 05:43 AM.. Reason: code tags
# 2  
Old 04-03-2015
You could try:
Code:
sed 's/^\(.\{8\}150\)8/\17/; s/^\(.\{8\}150\)9/\18/' file

(the order is important)

or if it is always the field value -1, try awk:
Code:
awk '{print substr($0,1,8) sprintf("%04d", substr($0,9,4)-1) substr($0,13)}' file

or gawk:
Code:
gawk '{$2=sprintf("%04d", $2-1)}1' FIELDWIDTHS="8 4 999" OFS= file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-03-2015
Perfect it works ... But I have one issue I have not posted my full file. My file will be having 1401,1402....1452,1501,1502...1509 all at 9-13 column. I have to replace one below values

Code:
1501 to 1453 
1502 to 1501
1503 to 1502
1504 to 1503
1505 to 1504
1506 to 1505
1507 to 1506
1508 to 1507
1509 to 1508

I have tried to replace the below sed to edit 1501 to 1453 . not working If you help me with that I can use the same sed to replace the 15XX values. Because your sed will repalce that and it not doing 1501 to 1453

Code:
sed 's/^\(.\{8\}150\)8/\17/; s/^\(.\{8\}150\)9/\18/' file

Thanks,
Arun

Last edited by arunkumar_mca; 04-03-2015 at 06:14 AM.. Reason: include the try I did
# 4  
Old 04-03-2015
Try
Code:
awk '{print $1, $1-($1==1501?48:1)}' file
1501 1453
1502 1501
1503 1502
1504 1503
1505 1504
1506 1505
1507 1506
1508 1507
1509 1508

# 5  
Old 04-03-2015
Below is my exact file. Sorry if I confussed . In the file I have to replace 1501 to 1453 which is in column 9 -12

Code:
000125701501150222000000568669021900010000004000027000192000176000100000000011220100000000011335
000125701501150222000000570933005900010000004000251007986000176000100000000011220100000025011335
000125701502150222000000616230004900020000005000017001131000195000100000000011220100000000011335
000125701503150222000000626722013900010000007000152001268000227000100000000011220100000018011335
000125701504150226000000003027003900012050010000015000000000247000100000000012370100000000051943
000125701505150226000000075563007900010000005000408001295000195000100000000012370100000011051943
000125701505150226000000133908002400020000002000779001791000127000100000000012370100000000051943
000125701506150226000000137040005300010000010000834005371000247000100000000012370100000000051943
000125701507150226000000549713003000060000000280637000029000023001000000000012370100000033051943
000125701507150226000000581895019900010000006400079008895000219000400000000012370100000000051943
000125701509150226000000583777019900010000012001168010016000249000800000000012370100000000051943
000125701509150226000000603925008500010000002500407000778000140000100000000012370100000000051943
000125701509150227000000201846005000010000000000158000000000000000100000000012370100000000060934

# 6  
Old 04-03-2015
Yes, understood. How about combining Scrutinizer's solution with my proposal?
# 7  
Old 04-03-2015
Tried combining . It is replacing as 0048. Can you pl guide me how to out across to achieve the result
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing values into a single column. sed/PERL

Hello everyone, i need to replace in the second column of my csv file, points by nothing and dash by comma like this: Input: 1 2 1;12.111.312-2;1.2;2;1-3 2 1 1;11.212.331-1;3.3;1;2-2 Output: 1 2 1;12111312;2;1.2;2;1-3 2 1 1;11212331;1;3.3;1;2-2 SED or PERL commands preferably. ... (7 Replies)
Discussion started by: satir
7 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. UNIX for Dummies Questions & Answers

Replacing values in a column if they equal a certain value

Hi, I have a tab delimited text file where some lines have the string "NA" in the second column. For these lines, I want to replace NA with the value in the first column, the symbol underscore followed by the value in the fourth column. How do I go about doing that? Thanks! Input: 1 ... (3 Replies)
Discussion started by: evelibertine
3 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

replacing negative values in a column with zero

Hi, i need help on replacing negative values in a column with 0. any quick fix on this? thanks much. for instance, input: 1 2.3 -0.4 -25 12 13 45 -12 desired output 1 2.3 0 0 12 13 45 (4 Replies)
Discussion started by: ida1215
4 Replies

6. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

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

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

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

10. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies
Login or Register to Ask a Question