0

I've been using this perl script to get a token out of stream on command line

  1 #!/usr/bin/perl
  2 
  3 $i = $ARGV[0];
  4 
  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 | token.pl 0
1664011
mjeong@login:~/bin$ qstat | grep XXX.0.par | token.pl 1
0.00063

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)

Thanks,

2 Answers 2

3

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

mjeong@login:~/bin$ qstat | grep XXX.0.par | cut -d ' ' -f 1
1664011
mjeong@login:~/bin$ qstat | grep XXX.0.par | cut -d ' ' -f 2
0.00063
1

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}'
1664011
$ qstat | awk '/XXX.0.par/ {print $1}'
0.00063

You can also do this fairly simply in Bash:

$ line=($(qstat | grep XXX.0.par)); echo ${line[0]}
1664011

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .