So say I've got a file with the following text in it.
And the source file I'm reading is like this:
#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
The following is the script I'm using
#!/bin/bash
opFile="opfile.txt"
sourceFile="TestOutput"
if [[ ! -e "$opFile" ]]
then
{
touch $opFile
count=0
grep "#" "$sourceFile" | while read -r line ; do
{
cpu=$(grep "#$count" "$sourceFile" | cut -f 4 | cut -d% -f1)
mem=$(grep "#$count" "$sourceFile" | cut -f 5 | cut -dM -f1)
disk=$(grep "#$count" "$sourceFile" | cut -f 6 | cut -dM -f1)
echo -e "$cpu\n$mem\n$disk" >> "$opFile"
((count++))
}
done
}
else
{
count=0
lineCounter=0
grep "#" "$sourceFile" | while read -r line ; do
{
cpu=$(grep "#$count" "$sourceFile" | cut -f 4 | cut -d% -f1)
mem=$(grep "#$count" "$sourceFile" | cut -f 5 | cut -dM -f1)
disk=$(grep "#$count" "$sourceFile" | cut -f 6 | cut -dM -f1)
((lineCounter++))
sed ""$lineCounter"s/$/,"$cpu"/" "$opFile" | tee "$opFile"
((lineCounter++))
sed ""$lineCounter"s/$/,"$mem"/" "$opFile" | tee "$opFile"
((lineCounter++))
sed ""$lineCounter"s/$/,"$disk"/" "$opFile" | tee "$opFile"
((count++))
}
done
}
fi
Now, the script needs to be run multiple times on that $sourceFile since the numbers in that file would keep changing. So the first time the script runs, the output is so
9.1
779.9
156.3
12.1
339.9
126.3
And the second time it runs (assuming the values are the same in the source file), the output should look like.
9.1,9.1
779.9,779.9
156.3,156.3
12.1,12.1
339.9,339.9
126.3,126.3
Now, the sed line I've used is correct, I'm pretty sure, but I'm having issues when it comes to putting that in the file. I could've used the >> output redirecter but that's printing everything on a new line. Tee is working slightly unexpectedly, sometimes it'd doing the right thing, other times, my opfile.txt is empty. Any idea on how to put my sed output in the file correctly? And no, preferably, I wouldn't want anything displayed on the stdout.
Thanks!