How to update field value in place?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to update field value in place?
# 1  
Old 03-04-2013
How to update field value in place?

Dear all:

I have a file:
Code:
1:00    2:abc   3:12aswe

and I ran the following awk script on this file:

Code:
#!/usr/bin/awk -f
{ i= 1;
while(i<=NF)    {
                $i=substr($i, 1, index($i, ":")-1);
                i++
                }
}

I am expecting the file would become (after running the script):
Code:
1 2 3

However, it didn't. How should I change the code to make it work?
# 2  
Old 03-04-2013
Awk is a filter, it doesn't modify the input. You should redirect into a temporary file, then overwrite the original.
How about:
Code:
cp $input ${input}.copy
awk -F: '{print $1}' RS="[ \t]+" ORS=" " $input > ${input}.tmp
mv ${input}.tmp $input

This User Gave Thanks to mirni For This Post:
# 3  
Old 03-04-2013
Another method:
Code:
awk '{ gsub(/:[^ ]*/,x); $1=$1 }1' input_file > input_file.tmp

mv input_file.tmp input_file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 03-04-2013
There is no default print if the main loop consists of only {}
You must add a print command. Lazy people do a 1, that's a true condition where the default action is again a print.
For demonstration I added all three possible print commands.
Code:
#!/usr/bin/awk -f
{ i= 1;
while(i<=NF)    {
                $i=substr($i, 1, index($i, ":")-1);
                i++
                }
print
}
{print}
1

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 03-04-2013
Thank you very much.

By the way, can perl update the field value in place? thanks.
# 6  
Old 03-04-2013
Yes, perl -i ... does it.
Here perl seems to be preferable anyway.
Code:
perl -pe 's/:\S*//g' file


Last edited by MadeInGermany; 03-04-2013 at 06:43 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 03-04-2013
With perl you can specify a backup extension after the -i switch:
Code:
perl -i.bak 's/:\S*//g' file #will save the original as file.bak

GNU sed can also edit in-place, here is sed solution:
Code:
sed -i 's/:[^ \t]*//g'

This User Gave Thanks to mirni For This Post:
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 update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to update value in field of out file using contents of another Ask

In the out.txt below I am trying to use awk to update the contents of $9.. If $9 contains a + or - then $8 of out.txt is used as a key to lookup in $2 of file. When a match ( there will always be one) is found the $3 value of that file is used to update $9 of out.txt separated by a :. So the... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

awk to update field in file2 if not the same as file1

Trying to use awk to: update $2 in file2 with the $2 value in file1, if $1 in file1 matches $13 in file2, which is tab-delimeted. The $2values may already be the same so in that case nothing happens and the next line is processed. There are exactly 4,605 unique $13 values. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Update a field using awk and keep the formatting.

Look at this simple example. echo " 2 4 6" | awk '{$2+=3;$3-=1}1' 2 7 5 Is there a simple way to update a field and at the same time keep the formatting? I would like to get it like this 2 7 5 I have tested both sub and gsub, it reformat too. (2 Replies)
Discussion started by: Jotne
2 Replies

6. Shell Programming and Scripting

awk to place value at 24 field in a flat file issue

I am trying to add 0393 value at 24th feild using the below command, but its adding at all the lines including header and trailer Input file: ZHV|2657|D0217001|T|TXU|Z|PAN|20131112000552||||OPER| 754|52479| 492|489|SP40|1014570286334|20131111|20131201|14355334|CHAMELON... (1 Reply)
Discussion started by: Aditya_001
1 Replies

7. Shell Programming and Scripting

Update field value on a csv file

Hi I have a job status csv file. I want to update the status of the job in the file. Below is the csv file 1,jobname1,in_progress,starttime,somthing,somthing 2,jobname2,completed,starttime,somthing,somthing 3,jobname3,failed,starttime,somthing,somthing... (8 Replies)
Discussion started by: midhun19
8 Replies

8. Shell Programming and Scripting

Copy a field into n line in another place

I have a file contains 100 section (here i show 2 section of that); i want to replace date from 1st & 2nd field of 1st line of each section (1st line of each section is header and the other lines are body) and merg them into one field, then copy it on 7th field of the lines in each section... (17 Replies)
Discussion started by: oreka18
17 Replies

9. Shell Programming and Scripting

update field

Hi All, i have a file file.dat with following data : CONTENT_STORAGE PERCENTAGE FLAG: storage_02 64% 0 storage_01 17% 1 i need to update FLAG field of storage_02 to 1 i am using the following: but its not working : awk '{FS=OFS=" "}$1==$defaultStorage1{$3="1"}1'... (13 Replies)
Discussion started by: kichu
13 Replies

10. UNIX for Dummies Questions & Answers

Update text files in place (string substitution) ??

The auditors have nailed us for world writeable files.... Apparently in years gone by, quite a number of our kornshell scripts have had: umask 000 put in the script. We have been able to turn off world writeable for existing dirs & files, but as these scripts run, new files keep getting... (1 Reply)
Discussion started by: kornshellmaven
1 Replies
Login or Register to Ask a Question