change a field in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change a field in a file
# 8  
Old 08-22-2009
Quote:
Originally Posted by malcomex999
when i run your code, it changes only on the second row on the second column. how about on the last one, there is 654???

Code:
>awk '{for(i=0;++i<NF;){if($i==654)$i+=2}}1' file
123 456 789
987 656 321
147 258 369
222 333 654

Code:
i<NF

should be:

Code:
i<=NF

Smilie
# 9  
Old 08-22-2009
replace
Code:
++i<NF

by ++i<=NF
# 10  
Old 08-22-2009
One missing char and the result is different.
Code:
awk '{for(i=0;++i<=NF;){if($i==654)$i+=2}}1' file

My bad Smilie
# 11  
Old 08-23-2009
Data Save

ok.
Why it can't make changes inside the file?Smilie
# 12  
Old 08-23-2009
awk script doesnt modify the actual file, you have to save it to another file and rename it to your original file.
# 13  
Old 08-24-2009
you just want to change every 654 to 656?

oh,my god....

Code:
sed 's/\b654\b/656/g' yourOldfile > newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to change value in field according to another

I am trying to use awk to check if each $2 in file1 falls between $2 and $3 of the matching $4 line of file2. If it does then in $5 of file2, exon if it does not intron. I think the awk below will do that, but I am struggling trying to is add a calculation that if the difference is less than 10,... (27 Replies)
Discussion started by: cmccabe
27 Replies

2. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

How to change variable length field?

Hello, I have a file with a date field with various lengths. For example: m/d/yyyy hh:mm or h:mm mm/dd/yyyy hh:mm or h:mm Is there a way using sed or awk to change the field to m/d/y ? I don't need the hours and minutes in that field, just the date in the proper format. Thanks in... (6 Replies)
Discussion started by: sonnyo916
6 Replies

4. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

5. Shell Programming and Scripting

Change a field value in a loop

Hi guys, i have an executable file that contains several records and fields. One of the records has a variable filed that must be changed each time i want to execute the file. Would it be possible that i can use a loop to change the value of that field? Suppose that the field address is: Record... (5 Replies)
Discussion started by: saeed.soltani
5 Replies

6. Shell Programming and Scripting

Split file when the key field change !

Hello, I have the following example data file: Rv.Global_Sk,1077.160523,D,16/09/2011 Rv.Global_Sk,1077.08098,D,17/09/2011 Rv.Global_Sk,1077.001445,D,18/09/2011 Rv.Global_Sk,1072.660733,D,19/09/2011 Rv.Global_Sk,1070.381557,D,20/09/2011 Rv.Global_Sk,1071.971747,D,21/09/2011... (4 Replies)
Discussion started by: csierra
4 Replies

7. UNIX for Advanced & Expert Users

change field 2 date format

from this input WEBELSOLAR,29122009,1:1 WIPRO,15062010,2:3 ZANDUREALT,18012007,1:3 i want output as WEBELSOLAR,20091229,1:1 WIPRO,20100615,2:3 ZANDUREALT,20070118,1:3 basically input is in ddmmyyyy format and i was to convert it to yyyymmdd format (1 Reply)
Discussion started by: manishma71
1 Replies

8. Shell Programming and Scripting

change field content awk

I have a line like this: I want to move HTTP/1.1 200 OK to the next line and put a blank line between the two lines i.e. How can i get it using awk? Thanks in advance (2 Replies)
Discussion started by: littleboyblu
2 Replies

9. Shell Programming and Scripting

insert EOD if value in Field 1 change

How to create simple awk program data such as below: input: 1 23 1 34 1 12 2 10 2 11 2 12 3 11 3 12 3 13 expected output: 1 23 1 34 1 12 EOD 2 10 2 11 2 12 EOD (2 Replies)
Discussion started by: jmaskar
2 Replies

10. Shell Programming and Scripting

How to change field seperator

Hi Please help me out with this problem: I want to have a script that would change the nth field seperator in a line into something else. like a,d,4,2,97,8,9 into a,d,4,2,97/8/9 Thanks (2 Replies)
Discussion started by: onthetopo
2 Replies
Login or Register to Ask a Question