change a field in a file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change a field in a file
# 1  
Old 08-19-2009
Question change a field in a file

Hi, All
I want to change a field in the middle of a file such as below

123 456 789
987 654 321
147 258 369

for example i extract 654 in line 2 with following code and sum with 20
EX=`cut -f2 -d " " FILENAME`
let EX=$EX+2

if we have too many fields with too many lines, how we can extract
(n,m) [n=line number , m= field number] of this file and replace it with
an other number, with out rewrite all file?

Last edited by mjelecom; 08-19-2009 at 06:24 AM..
# 2  
Old 08-19-2009
What are the constants of the file?

Will it always be line 2, field 2?
# 3  
Old 08-19-2009
Your question is not quite clear but try if this is what you wanted...
654 will be the number which u will be searchin in each field one by one and it will add 2 when it find the match for 654

awk '{for(i=1;i<=NF;i++){if($i==654)$i=$i + 2; print $i}}' infile | paste - - -

Last edited by malcomex999; 08-19-2009 at 01:39 PM..
# 4  
Old 08-19-2009
So Advanced, Thank you
# 5  
Old 08-22-2009

It was powefull, and can any body tell me how can i save only this cell , without need to overwrite all file?
Assume that, there is too many records (a huge file).
# 6  
Old 08-22-2009
Shorter Smilie
awk '{for(i=0;++i<NF;){if($i==654)$i+=2}}1' file

# 7  
Old 08-22-2009
Originally Posted by danmero
Shorter Smilie
awk '{for(i=0;++i<NF;){if($i==654)$i+=2}}1' file

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

>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

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