update field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting update field
# 1  
Old 12-07-2010
Question update field

Hi All,

i have a file file.dat with following data :

Code:
 
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 :

Code:
 
awk '{FS=OFS=" "}$1==$defaultStorage1{$3="1"}1' file.dat

but its not getting updated

But if i am passing the value directly as below it is getting updated

Code:
awk '{FS=OFS=" "}$1=="storage_02"{$3="1"}1' file.dat


Please help me ..

thanks in advance
# 2  
Old 12-07-2010
Try:
Code:
awk -vx=$defaultStorage1 '{FS=OFS=" "}$1==x{$3="1"}1' file.dat

# 3  
Old 12-07-2010
Question

i found the reson as to why itsnot working:

The file has value as
Code:
CONTENT_STORAGE PERCENTAGE FLAG:
/storage_02 64% 0
/storage_01 17% 1

where as $defaultStorage1 has value as storage_02

What needs to be done to check if the CONTENT_STORAGE is /storage_02 and update FLAG as "1"

Thanks in advance

Last edited by Scott; 12-07-2010 at 07:57 AM.. Reason: Code tags
# 4  
Old 12-07-2010
Code:
awk '{FS=OFS=" "}$1=="/${defaultStorage1}"{$3="1"}1' file.dat


Last edited by Scott; 12-07-2010 at 07:57 AM.. Reason: Code tags
R0H0N
# 5  
Old 12-07-2010
Thanks all..

can you also let me know how to append a "\"to defaultStorage1 and check if $1 is equal to that ?

defaultStorage1 variable has value storage_02

but in file the 1st column is /storage_02?
# 6  
Old 12-07-2010
Code:
defaultStorage1="\/storage_02"


Last edited by Scott; 12-07-2010 at 07:58 AM.. Reason: Code tags
R0H0N
# 7  
Old 12-07-2010
i tried

Code:
 
awk '{FS=OFS=" "}$1=="\/${defaultStorage1}"{$3="1"}1' file.dat

but all it is not updating the value

awk: cmd. line:1: warning: escape sequence `\/' treated as plain `/'
CONTENT_STORAGE PERCENTAGE FLAG:
/storage_02 64% 0
/storage_01 17% 1

---------- Post updated at 05:42 PM ---------- Previous update was at 05:35 PM ----------

Hi,

Code:
 
awk -vx="\/$defaultStorage1" '{FS=OFS=" "}$1==x{$3="1"}1' file.dat

is updating but its not writing to file.dat

i am getting echo output as

Code:
awk: warning: escape sequence `\/' treated as plain `/'
CONTENT_STORAGE PERCENTAGE FLAG:
/storage_02 64% 1
/storage_01 17% 1

but the file still has value
Code:
 
 
CONTENT_STORAGE PERCENTAGE FLAG:
/storage_02 64% 0
/storage_01 17% 1

thanks in advance
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

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

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

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

How to update field value in place?

Dear all: I have a file: 1:00 2:abc 3:12asweand I ran the following awk script on this file: #!/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... (7 Replies)
Discussion started by: littlewenwen
7 Replies

9. Shell Programming and Scripting

Update specific field in a line of text file

I have a text file like this: subject1:LecturerA:10 subject2:LecturerA:40 if I was given string in column 1 and 2 (which are subject 1 and LecturerA) , i need to update 3rd field of that line containing that given string , which is, number 10 need to be updated to 100 ,for example. The... (6 Replies)
Discussion started by: bmtoan
6 Replies

10. Shell Programming and Scripting

Update a field in a file based on condition

Hi i am new to scripting. i have a file file.dat with content as : CONTENT_STORAGE PERCENTAGE FLAG: /storage_01 64% 0 /storage_02 17% 1 I need to update the value of FLAG for a particular CONTENT_STORAGE value I have written the following code #!/bin/sh threshold=20... (1 Reply)
Discussion started by: kichu
1 Replies
Login or Register to Ask a Question