Server Fault is a question and answer site for system and network administrators. 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've been using this perl script to get a token out of stream on command line

  1 #!/usr/bin/perl
  3 $i = $ARGV[0];
  5 while (<STDIN>) {
  6   @tokens = split /\s+/;
  7   print $tokens[$i] . "\n";
  8 }

typical usage would be something like

mjeong@login:~/bin$ qstat | grep XXX.0.par
1664011 0.00063 XXX.0.par mjeong       qw    11/05/2010 10:34:37  16        
mjeong@login:~/bin$ qstat | grep XXX.0.par | 0
mjeong@login:~/bin$ qstat | grep XXX.0.par | 1

Out of curiosity, is there a standard utility to do this with similar(=simple) interface - so that I don't have to write this little script for any new machine? I understand Awk/Sed can do this, but regex syntax is a little complicated to type every time (I need no brainer syntax, since I use this a lot)


share|improve this question
up vote 3 down vote accepted

cut works exactly the same, with very little addition of complexity

mjeong@login:~/bin$ qstat | grep XXX.0.par | cut -d ' ' -f 1
mjeong@login:~/bin$ qstat | grep XXX.0.par | cut -d ' ' -f 2
share|improve this answer

The AWK command to do that doesn't require a regular expression, but it can use one to do the grep part, too:

$ qstat | awk '/XXX.0.par/ {print $1}'
$ qstat | awk '/XXX.0.par/ {print $1}'

You can also do this fairly simply in Bash:

$ line=($(qstat | grep XXX.0.par)); echo ${line[0]}
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.