1

new to Ubuntu so sorry if this has already been asked but I would like a way to parse a text file that has a general format of:

-------- step 0  ---- cpu =     Time_value  -------
Energy =  Energy_value1   KinEng = KinEng_value1 Temp = Temp_value1

-------- step 10 ---- cpu =     Time_value  -------
Energy =  Energy_value2   KinEng = KinEng_value2 Temp = Temp_value2

Specifically I was trying to figure out how to use awk and/or grep to pull out the time value and temp_value and output them to a file in individual columns to be

Time_value1 Temp_value1
Time_value2 Temp_value2
etc...

Looking up awk documentation I found that awk '/Temp/ {print $9}' file_name would give me the temp value and that awk '/cpu/ {print $7}' file_name should give me the time_value but how can I search for both strings in one command while searching different columns for their respective strings. In other words how can I alter the line awk '/cpu|sec/ {print}' file_name to include the column information for each string.

@steeldriver: Tbh the text file is even harder to read in an actual editor because its format but I'll attach a screenshot of the 'cleanest' view of it.

enter image description here

  • Can you give an actual (minimal) example of the file? it's hard to tell what / where the fields are exactly based on what you have posted – steeldriver Jul 14 '18 at 19:40
  • 2
    For future, please avoid posting images of text. It makes it difficult to work with actual data for the answerers. See also meta.askubuntu.com/a/8714/295286 – Sergiy Kolodyazhnyy Jul 14 '18 at 21:42
  • What's the reason you haven't accepted any of the proposed answers? – simlev Jul 24 '18 at 10:01
  • Sorry I'm new to this forum and up until I just read this I didn't know that was a thing and had to look up how to do it. – Jonathan Tran Jul 26 '18 at 2:18
  • @JonathanTran It's totally OK. Accepted answer simply marks most suitable solution or signifies that you as question owner got what you wanted. It's not a requirement to accept and is question owner's choice, but does help communicating if things are OK or not. Welcome to the site, and have fun with Ubuntu ! – Sergiy Kolodyazhnyy Jul 26 '18 at 3:02
0

The awk you want should be as follows:

awk -F '=' '/^-/{gsub(/\-*$/,"",$2);print $2}' input.txt

The idea here is that we're using = as column ( or in awk terminology - field ) separator. As such, in your desired line that contains CPU time, you have only one =, which would make everything on the left filed $1 and on the right of it - $2.

After that, it's the simple /PATTERN/ {ACTION} structure. Only lines that match the pattern of starting with a dash,will have the ending dashes cut off, and then what remains is the CPU time.

  • This more or less worked. Right now I'm getting the CPU value output and temperature output I want using the code you gave but is there a way to tell awk to print a particular pattern into the 1st column of the output and the 2nd pattern into the second column? I want it printed to an output file in just two columns one with time and one with temperature so its easy to plot. Right now it searches for each pattern on every line and outputs it like: time, temp , time ,temp all in one column – Jonathan Tran Jul 16 '18 at 19:00
  • @JonathanTran Sure, just reverse the order print $2 $1 – Sergiy Kolodyazhnyy Jul 16 '18 at 19:07
0

Your "units" are groups of 5 lines. In this situation it may be useful to:

awk '{print $1, $11}' RS="cpu =" logfile

Where RS="cpu =" redefines the record separator (RS) as "cpu =". Then it is just a question of printing the desired fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.