search file, change existing value based on input (awk help)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search file, change existing value based on input (awk help)
# 1  
Old 12-05-2006
search file, change existing value based on input (awk help)

I have a file (status.file) of the form:

valueA 3450
valueB -20
valueC -340
valueD 48

I am tailing a data.file, and need to search and modify a value
in status.file...the tail is:

tail -f data.file | awk '{ print $3, ($NF - $(NF-1)) }'

which will produce lines that look like this:

valueB -40
valueD 220
valueA -100

so I think what I would like to do is add a pipe to the tail command,
and send those lines to an awk command/program. It should then
search through status.file, and add the value to the correct line.

For example, using the above status.file, a line of data (after having
gone through the tail line) of
valueC -30

would result in status.file being changed to:

valueA 3450
valueB -20
valueC -370
valueD 48


Any help/tips would be greatly appreciated, thanks
# 2  
Old 12-05-2006
Code:
tail -f data.file | awk ' 
BEGIN { 
      while ( getline < "status.file" )  { arr[$1]=$2 } 
}
{      if ( arr[ $3 ] != "" ) { arr[$3] = arr[$3] + $NF - $(NF-1)  }    }
END { 
       for( key in arr ) print key,arr[key] 
}'

# 3  
Old 12-05-2006
Quote:
Originally Posted by anbu23
Code:
tail -f data.file | awk ' 
BEGIN { 
      while ( getline < "status.file" )  { arr[$1]=$2 } 
}
{      if ( arr[ $3 ] != "" ) { arr[$3] = arr[$3] + $NF - $(NF-1)  }    }
END { 
       for( key in arr ) print key,arr[key] 
}'


Hi,

Thanks for responding, I appreciate it.

It looks like this does what I want, except it doesn't save the new results in status.file. I've saved the code above (from "BEGIN" to "key]}") into adjust_results.awk, and when I do this:

tail data.file | awk -f adjust_results.awk

the output is correct...but the change hasn't been saved in status.file.
# 4  
Old 12-06-2006
you are trying to save output to replace the existing status.file?

tail data.file | awk -f adjust_results.awk > status.sfile

redirect the output to status.file

(not sure if that's what you're looking for ? ) =)
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 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

2. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

3. Shell Programming and Scripting

How to create file and file content based existing information?

Hi Gurus, I am SQL developer and new unix user. I need to create some file and file content based on information in two files. I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is... (13 Replies)
Discussion started by: Torhong
13 Replies

4. Shell Programming and Scripting

Change date time stamp of existing file

I have a file hello.txt which was created today (today's date timestamp) I wish to change its date timestamp (access, modified, created) to 1 week old i.e one week from now. uname -a SunOS mymac 5.11 11.2 sun4v sparc sun4v Can you please suggest a easy way to do that ? (12 Replies)
Discussion started by: mohtashims
12 Replies

5. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

6. Shell Programming and Scripting

Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range. I dont know how to go about this. I am hoping that the... (5 Replies)
Discussion started by: lostincashe
5 Replies

7. Shell Programming and Scripting

generate a file from existing file based on some rule

Hi guys, i have a file in below format.let me explain first what i am trying to do . i am making a file from this file , will rearrange these columns, then i will run several command(start/stop mentioned in this file) on unix environment. my requirements : 1. i have 27 servers , on each server... (4 Replies)
Discussion started by: deepakiniimt
4 Replies

8. Shell Programming and Scripting

Change in Input feed based on condition file

Sorry Guys for not being able to explain in one of my earlier post. I am now putting my requirement with the input file and desired output file. In the below input file - Transaction code is at position 31:40. Business code is from position 318:321 TSCM00000005837 ... (7 Replies)
Discussion started by: varunrbs
7 Replies

9. Shell Programming and Scripting

Creating a csv file based on Existing file

Hi I am Newbie to Unix.Appreciate Help from forum user would loada b.Csv File(Below example) in /data/m/ directory.Program need to read the b.csc to extract certain column and create a new file /data/d/ directory as csv file with new name. User File Format 1232,samshouston,12345... (3 Replies)
Discussion started by: skywayterrace
3 Replies

10. UNIX for Dummies Questions & Answers

how can i change the date of an existing file

Hi, Plz suggest me how can i change the date of a file. Suppose my file has been created in some date and i want to give it present date. How can i do this???? (2 Replies)
Discussion started by: adityam
2 Replies
Login or Register to Ask a Question