I want to make the command below work but I can't. Please check this out:

echo "random string random string mark: abcde random string random string" |
    awk '{gensub(/^.*mark: (.{5,5}).*$/,"\\1","g"); print}'

I want this command to return only "abcde". But it is always returning the entire input string. Why?

  • The print command has no argument, so it prints the whole line. Use something like "a=gensub(...)" and then "print a".
    – LouisB
    Nov 1, 2015 at 15:21
  • @Louis why not make that an answer?
    – roaima
    Nov 1, 2015 at 16:24
  • 1
    @roaima Well, I thought it was more like debugging or a syntax problem. In fact, I expected the question to be put on hold as being off topic for that reason. On the one hand, the fix was simple enough and I wanted to help. On the other, I didn't want to look like I was chasing points by posting some trivial fix as an answer that doesn't really have anything to do with Unix/Linux. The answers that are posted are much more useful than any fix I could come up with. Are my cynicism and pessimism showing? But thanks for your encouraging comment. Next time I will try to post an answer.
    – LouisB
    Nov 1, 2015 at 20:20
  • @Louis, post an answer and include an explanation of why it works and/or why you've chosen to implement the solution in that particular way...perhaps also an explanation of why what the OP posted didn't work.
    – cas
    Nov 2, 2015 at 0:51

2 Answers 2


The command gensub does not change the string. The result is an output of the function.

Please try:

echo "random string random string mark: abcde random string random string" |
awk '{print(gensub(/^.*mark: (.{5,5}).*$/,"\\1","g"))}'


Turns out that the user needs parsing top output, which this does (as the user asks):

top -b -n 1 -d 1 |
awk 'NR==1{print(gensub(/.*load average: ([0-9\.\, ]+).*$/,"\\1","gs"))}'

Note: It is allowing both the dot and the comma for locales that use a comma as decimal separator.

Please take a look at mpstat as presented here, it may be a better fit for your needs.

  • hi, acutally your code works perfectly! Really good but for reason I cant match multine with it. I actually need to use exactly your code (with awk and gensub) to get the load average of the "top" command. I know I can make it with many other ways, but I want to use regex and your really nice code. So far I have this code that does not work (even with your suggestion): top -b -n 1 -d 1 | awk '{print(gensub(/^.*load average: ([0-9\. ]*).*$/,"\\1","gs"))}'
    – Samul
    Nov 1, 2015 at 18:02
  • worked exaclty as I wanted! Do you mind to tell me why does the NR solved the problem? And why cant I use "gensubs(.......);printf $0" ?
    – Samul
    Nov 1, 2015 at 18:19
  • @Samul NR is the record number, so, only when the record number is 1 (the first line) is the command executed. The value of the $0 is not changed by gensub, so, if you print $0, you get an un-changed $0. You need to print the "output" of gensub, and that is exactly what the command does.
    – user79743
    Nov 1, 2015 at 18:25
  • You are awesome @binaryZebra! You are really awesome. Thank you so much. Do you mind helping me answer this last question related to this post? -> unix.stackexchange.com/questions/240136/…
    – Samul
    Nov 1, 2015 at 18:40
  • 1
    use uptime if you just want the load average. parsing top's output is crazy. this question was answered yesterday: unix.stackexchange.com/questions/239952/…
    – cas
    Nov 2, 2015 at 0:55

To rephrase your question: You want to print the first word after the string "mark: ".

On way, in awk:

echo "random string random string mark: abcde random string random string" | \
awk -v RS="mark: " 'NR==2{print $1}'

The above splits the string into two records separated by "mark: ", and then prints the first word of the second record (which is "abcde").

In sed:

echo "random string random string mark: abcde random string random string" | \
sed 's/.*mark: \([^ ]*\).*/\1/'

The above uses regular expressions to find the first non-space word after the "mark: ", and replaces the whole string with that word.

  • your code worked fine too! But how do I make it work if my "random string" has multilines on it? You code didnt work if I used multilines.
    – Samul
    Nov 1, 2015 at 18:09
  • 1
    @Samul you didn't mention anywhere in your question that you needed this to work across multiple lines
    – roaima
    Nov 1, 2015 at 20:23

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.