Reputation: 1163
I've got an output file that's already gotten the following values in it
16.1
144.3
352.5
23.4
129.5
993.9
I've also got a source file with the following text in it.
#0 abc 2016-08-06 01:12:57 AM 9.1% 779.9M of 1G 156.3M of 1G
#1 abc 2016-08-06 02:33:47 AM 12.1% 339.9M of 1G 126.3M of 1G
Using grep, I run a loop over the #0 and #1 lines and store the following in variables on the first iteration.
$cpu #stores 9.1
$mem #stores 779.9
$disk #stores 156.3
The next time, my grep loop runs over the "#1" line, my 3 variables will update to 12.1, 339.9 and 126.3.
I'm trying to use sed to get the output as follows
16.1,9.1
144.3,779.9
352.5,156.3
23.4,12.1
129.5,339.9
993.9,126.3
I'm using the following sed code in a loop to do this.
sed ""$counter"s/$/,"$cpu"/" "$opFile" | tee "$opFile"
((counter++))
sed ""$counter"s/$/,"$mem"/" "$opFile" | tee "$opFile"
((counter++)
sed ""$counter"s/$/,"$disk"/" "$opFile" | tee "$opFile"
The reason why I'm using tee is because without it, I get the output like so when run in a script
16.1,9.1
144.3
352.5
23.4
129.5
993.9
16.1
144.3,779.9
352.5
23.4
129.5
993.9
and so on.. I hope you see what's happening.
Tee provides me the right answer but only works sometimes but with very odd behaviour. On the command line, sometimes it appends to all 6 lines correctly. Another time, it appended to 5 lines and then just made the opFile empty. And another time, on the first try itself it made the opFile empty. Forget command line, at the end of executing my script, everytime my opFile is blank.
Why does this happen?
Upvotes: 0
Views: 80
Reputation: 23667
$ cat output.txt
16.1
144.3
352.5
23.4
129.5
993.9
$ cat source.txt
#0 abc 2016-08-06 01:12:57 AM 9.1% 779.9M of 1G 156.3M of 1G
#1 abc 2016-08-06 02:33:47 AM 12.1% 339.9M of 1G 126.3M of 1G
$ paste -d, output.txt <(grep -oP '[0-9.]+(?=%)|[0-9.]+(?=[A-Z]+ of)' source.txt)
16.1,9.1
144.3,779.9
352.5,156.3
23.4,12.1
129.5,339.9
993.9,126.3
This alternate solution might work, I don't know what's wrong in your approach...
Edit:
$ paste -d, output.txt <(grep -oP '[0-9.]+(?=%)|[0-9.]+(?=[A-Z]+ of)' source.txt) > tmp ; mv tmp output.txt
This will save output in tmp
file and then moved to output.txt
Upvotes: 3
Reputation: 74595
I haven't completely followed what you're trying to do but you shouldn't be reading from and writing to the same file - this is likely to be where your problem lies.
When you specify the output file to tee
, it will be opened for writing and truncated. If sed
gets a chance to read the file before this happens, then things will appear to be working. However, this won't always be the case.
Chances are that your overall process can be simplified but using a temporary file should at least fix the problem.
For example:
sed ""$counter"s/$/,"$disk"/" "$opfile" | tee "${opfile}_tmp" && mv "${opfile}_tmp" "$opfile"
Upvotes: 2