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.

  • That question doesn't make sense to me. could you please put it in context? What regexp tool are you using? 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
  • 1
    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

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"

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; 

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.