Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a pipelined output from free -o -m | awk '{print $4}' I want take step by step two numbers the first and the second line. So i need two regexes. I tried with ^[0-9]{1,3}$ but this match all occurences. i.e. 111 222 333

I want only 111 by first and 222 by second regex.

share|improve this question
That question doesn't make sense to me. could you please put it in context? What regexp tool are you using? – Stéphane Chazelas Feb 8 '13 at 16:03
Good question, i use bash. grep pipelined output. and it give me a bunch of numbers about memory. i want take the first occurence of \d{3} number check the condition, and then take the second occurence. With my actual regex i can do the task by use explode string into substrings but this is not an elegant method – Yurij73 Feb 8 '13 at 16:47
I am going to question the usefullness of taking only the mem line and the swap line and leaving out the buffer. The actual amount of free memory space is the free space in MEM plus the buffer space as the system dynamically drops buffer space when memory is needed...also will keep memory as fully employeed with buffers as much as possible. – mdpc Feb 8 '13 at 21:10
up vote 1 down vote accepted

Your question is a bit backward.

You probably want something like:

free -o -m | awk '
  $1 == "Mem:" {print "do something with " $4}
  $2 == "Swap:" {print "do something with " $4}'

Something like:

eval "$(free -o -m | awk '
  $1 == "Mem:" {print "free_mem=" $4}
  $1 == "Swap:" {print "free_swap=" $4}'
printf "%s\n" "Free mem: $free_mem" "Free swap: $free_swap"
share|improve this answer

I think I still don't understand completely what you're asking here. But free -o -m | awk '{print $4}' gives you output of the form:


To check 123 and 456 against a condition on bash you could use a for loop instead of fiddeling with regexes:

for value in `free -o -m | awk '{print $4}'`; do 
    if [[ "$value" != "shared" && CONDITION ]]; then echo "$value matched my condition"; fi; 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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