How can I find the value before the word free?top -n1 | grep Mem                   Mem:  2054968k av, 2034120k used,   20848k free,       0k shrd,  186768k buffFirst I tried   top -n1 | grep Mem  | awk '{print $7}'but this isnt good because the free value (here, 20848k) can be in different fields. Sometimes it's field 6, others field 7 and so on.I need this to work on both Linux and Solaris.
sed + find value before word in line
text processing;sed;awk;solaris
awk -v RS=[, ] '/free/{print a}{a=$0}'ExplanationSet the record separator to , and space, so the number preceding every string is a record in itself, and so is the string.Having everything as its own record, awk will process every item one by oneFor all the records before free it will ignore the {print a} because the condition doesn't match, and it will skip to {a=$0} which will store the currently processed record in variable aOnce /free/ is matched, awk will just {print a} where a contains the record right before the match