0

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.

4
  • 3
    Can you show your script? And can it be changed or this has to be done after the script execution? Commented Jul 8, 2020 at 15:10
  • 2
    So what have you tried? This should be trivial with awk. Commented Jul 8, 2020 at 15:12
  • It would be easier to extract this from whatever data the script is formatting, or at least have the script provide a more machine-parseable output format as an option. Commented Jul 8, 2020 at 15:15
  • This has to be done after script execution, the problem here is UNIT & FREE MEMORY for that unit are not in same line so formatting is bit difficult. Commented Jul 8, 2020 at 16:10

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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}'

Comments

0

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
  • Replace the 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

Comments

0

Just send your script's output into this awk command:

Your_Script | awk '
/^UNIT/ {printf $NF" "}
/^Free memory/ {print $NF}
'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.