When I run bjobs -w in the machine where I work, I get the next output:

821213  user1 RUN   parallel16 hpc2        16*pirineus NAME1111 Apr 9 20:26
823954  user1 RUN   parallel16 hpc1        5*pirineus4 NAME2222 Apr 11 22:45
824083  user1 RUN   parallel16 hpc1        16*collserola2 otherthing Apr 12 19:20
824297  user1 RUN   parallel16 hpc1        8*collserola10 another_long_name Apr 13 20:50
824305  user1 RUN   parallel16 hpc2        16*collserola5 Try_anothername Apr 14 12:16

Then, I want to get the information from the first and sixth column. For that, I thought I could do it like this

bjobs |awk '(NR>=2) {print $1 " "$6 }'

but then, I am getting

821213 16*pirineus
823954 5*pirineus
824083 16*collsero
824297 8*collsero
824305 16*collsero

when I would like to get

821213 pirineus
823954 pirineus4
824083 collsero2
824297 collsero10
824305 collsero5

I tried the next command (following the idea of using parameter operations) but it gave me an error.

bjobs |awk '(NR>=2) {print $1" " ${6#* *} }'

awk: (NR>=2) {print $1" " ${6#* *} }
awk:                       ^ syntax error
awk: línea ord.:1: (NR>=2) {print $1" " ${6#* *} }
awk: línea ord.:1:                         ^ syntax error

Any ideas on how can I solve this?

  • 1
    Why would awk treat 16*pirineus as two fields? – Hauke Laging Apr 14 '18 at 20:30

You can't use the shell's parameter substitution inside an awk program.

To strip out the first bit of the sixth column, use sub():

bjobs -w | awk 'NR > 1 { sub("^[^*]*[*]", "", $6); print $1, $6 }'

This would modify the sixth field by removing the bit up to the * before printing it. The regular expression ^[^*]*[*] matches any number of characters that are not * at the beginning of the string, and then the *. This is then replaced by the empty string.

The above transforms the output in the question to

821213 pirineus
823954 pirineus4
824083 collserola2
824297 collserola10
824305 collserola5

NOTE: All variations below (including the sed solution) assumes that there is a * in the sixth field. The above would work even if there wasn't.

Another way of doing it is to split the sixth field on the * and print the second part:

bjobs -w | awk 'NR > 1 { split($6, a, "[*]"); print $1, a[2] }'

A third way of doing it is using both blanks and * as the field delimiter (note the change in field number):

bjobs -w | awk -F "[[:blank:]*]+" 'NR > 1 { print $1, $7 }'

Using sed:

bjobs -w | sed -nE 's/^([[:alnum:]]+).*\*([[:alnum:]]+).*/\1 \2/p'
  • In all option the Sufix number of column 6 (e.g. collsero10 , pirineus4, ... ) is missing. Is there a way to keep it? – Seven77 Apr 14 '18 at 20:59
  • @Seven77 Um, they're not missing when I run with the data that you posted. I never touch that part of the string. – Kusalananda Apr 14 '18 at 21:04
  • @Seven77 Hmm... could it be that your output is from bjobs -w (first line of the question), but that you then use bjobs (no -w)? I was using the output that you posted, and if that's from bjobs -w then you should use bjobs -w with my solutions too. – Kusalananda Apr 14 '18 at 21:08
  • Yeah, you were right, I used bjobs instead of bjobs -w. Thanks for the solution. – Seven77 Apr 14 '18 at 23:04

I have used below command to get the desired output

awk 'NR >=2{print $1,$6}' example.txt  | sed "s/[0-9]\{1,2\}\*//g"


821213 pirineus
823954 pirineus4
824083 collserola2
824297 collserola10
824305 collserola5

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.