0

I have log files like this for some top data:

Mon May  9 23:45:02 EDT 2016
PID   USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
14816 radius    25   0  848m 415m  10m S   10  2.6 274:05.28 java

How does one get the first line and just the value for %MEM?

I have tried sed/awk/paste but not having any luck.

Somehow the data is not perfect in that I can't always trust the the value is the 21st spot.

5
  • what don't you trust about the 21st column? It looks to me like %MEM would be the 10th column, but are there lines where previous fields have spaces, throwing off the column alignment? Maybe there's another way to solve your problem, or are you set on post-processing top output?
    – Jeff Schaller
    May 10, 2016 at 23:04
  • I was using paste to make the first 3 lines of each file one big line and then am trying to get the 21st field as well as others. I think you're right the column alignment somehow might be off but I can't see the issue if I look at the source data (there are many source files). It 'looks' OK. My goal is to put all of the extracted into one big file.
    – jouell
    May 10, 2016 at 23:22
  • Edit your post and add the desired output... You want it in one line for each log file, right ? May 10, 2016 at 23:36
  • @jouell: still confused, if you want the memory percentage from concatenated lines the field would be 28?
    – tink
    May 11, 2016 at 8:51
  • @tink - you're right. I also ran into another problem, I had 2 different formats to contend with, so one column was 21 and the other 28. In the end this worked out using paste -d@ - - - to concatenate the first 3 lines and tr -s ' ' and the cutting and awking. I still really don't know why but I had extra spaces in the file so the tr slurped up the extra whitespace.
    – jouell
    May 11, 2016 at 23:59

2 Answers 2

2

An awk moment:

$ awk 'NR == 1 {print;} NR == 2 {print $10; exit;}' input
1
  • Try NR == 3, maybe? I don't think he wanted the static string %MEM to show up ....
    – tink
    May 11, 2016 at 8:44
0

You can try using head/tail and awk :

head -1 logfile 
tail -n +2 logfile | awk '{print $10}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .