Reputation: 16
I am having a script output data that needs to be formatted, I have a output like below.
UNIT : ABC
xxxx
xxxx
Free memory : xxx 70
UNIT : DEF
xxxx
xxxx
Free memory : xxx 60
I need to capture free memory data for individual units but I am unable to print that out, any suggestions ? There are some 25 units & I have to print free memories of all of them individually.
Here "xxxxx" is some random data which is irrelevant for me, what I want is to print free memory for each unit, something like below format :slight_smile
ABC 70
DEF 60
and so on.
Upvotes: 0
Views: 119
Reputation: 704
Just send your script's output into this awk command:
Your_Script | awk '
/^UNIT/ {printf $NF" "}
/^Free memory/ {print $NF}
'
Upvotes: 0
Reputation: 7781
With GNU ed
If it is a file.
ed -s file.txt < script.ed
If the output is coming from a script, requires a shell that supports <()
Process Substitution.
ed -s <(cat file.txt) < script.ed
cat
part with the output/command from your script.The script.ed
script.
v/^UNIT\|^Free/d
,s/.*[[:space:]]\(.*\)$/\1/g
,s/$/ /
g/./j
,s/ $//
,p
Q
Upvotes: 0
Reputation: 10103
Using GNU sed
/path/to/your/script |
sed -n -E '/^UNIT\s*:\s*/{s///;h}; /^Free memory\s*:.*\s([0-9]+)$/{s//\1/;H;g;s/\n/ /p}'
Upvotes: 0
Reputation: 1309
Let the file output.txt
contains the output,
Then Assuming, the order of UNIT and Free memory doesn't change in the output, we could try the following.
cat output.txt | grep -Ea 'UNIT|Free memory' | sed -e ':a;N;s/\nFree/ Free/g' | awk '{print $3" "$8}'
Output:
ABC 70
DEF 60
Explanation:
grep -Ea 'UNIT|Free memory'
: This will grep out only the lines contains either UNIT or Free memory.sed -e ':a;N;s/\nFree/ Free/g'
: This will loop through each line, and wherever new line starts with Free, the new line will replace with a space. Hence we will get UNIT and Free memory in a single line.awk '{print $3" "$8}'
: This further cut out the positions of UNIT name and Free memory value.Hope it helps.
Upvotes: 1