f



awk correct tool? (search file, change value based on input)

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
Edit/Delete Message

0
nortonloaf
12/5/2006 6:08:17 AM
comp.lang.awk 3450 articles. 0 followers. Post Follow

2 Replies
526 Views

Similar Articles

[PageSpeed] 5

nortonloaf@comcast.net wrote:
> 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
> Edit/Delete Message

Your problem description is vague, so the following might not be what 
you want, and it can create a lot of disk activity. Oh, and the status 
data will be unsorted.

	#!/bin/sh
	IN="data.file"
	STAT="status.file"
	[ -f "$STAT" ] || touch "$STAT"

	tail -f "$IN" | nawk '
		BEGIN { stat = ARGV[1] }
		FILENAME == stat { sum[$1] = $2; next }
		{
			sum[$3] = sum[$3] + $NF - $(NF-1)
			
			#
			# Rewrite status file from scratch.
			# This happens for each and every input line
			# and can create a lot of disk activity if the
			# data is coming in fast.
			# If this is a problem use a timer or counter
			# and output the data only every second or so
			#

			printf "" > stat
			# unsorted output
			for(i in sum) {
				print i " " sum[i] >> stat
			}
			close(stat)
		}
	' "$STAT" -


/Thomas
0
Thomas
12/5/2006 8:17:45 AM
=CF/=C7 nortonloaf@comcast.net =DD=E3=F1=E1=F8=E5:
> 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
>

Are you sure for this output? Maybe you tripped copy/paste.

> Any help/tips would be greatly appreciated, thanks
> Edit/Delete Message

Anyway, if your specification is correct, the following script should
do the job:

tail -f data.file | awk '{ print $3, ($NF - $(NF-1)) }' |
awk 'NR =3D=3D FNR { a[$1] =3D $2; next } { a[$1] +=3D $2; print $1, a[$1] =
}' -
status.file

printing modified values of status.file.

0
Vassilis
12/5/2006 8:43:35 AM
Reply: