0

I need to use the following grep command in a shell script:

grep 'DUE TO TIME LIMIT' ./slurm-3364.out

Which outputs the lines in slurm-3364.out containing the pattern 'DUE TO TIME LIMIT'. I want to replace the file name with a variable and affect the output to a another variable. So far I can't get the same result, I tried:

jobOver="$(grep 'DUE TO TIME LIMIT' "$NEWEST")"
echo $jobOver

With $NEWEST containing the appropriate file name. This produces a very long text output that seems to list every file in the working directory...

Edit: As mentioned in the comments, doing echo "$jobOver" solved the problem.

JohnND
  • 103
  • 1
  • 12
  • 1
    What you've written is certainly *a* correct Bash statement, and it does what you said you want. If what you want is not what that command does, you'll need to give us more precise information about what you want . . . – ruakh Feb 24 '16 at 16:36
  • 1
    Please show an abbreviated input and desired output. Define what you want and don't just say "this doesn't do what I want." How do we know what you want??? – dawg Feb 24 '16 at 16:36
  • Edited my post. Basicaly both examples give different output. – JohnND Feb 24 '16 at 16:51
  • Can you show the rest of the script and the value of `$NEWEST` ? I tested it and it seems to do what you expect. – slugo Feb 24 '16 at 17:04
  • 3
    quote the argument to echo `echo "$jobOver"`. Using my crystal ball, I can see that the file contains '*' characters in the matched lines. – William Pursell Feb 24 '16 at 17:07
  • Use redirection: `grep 'DUE TO TIME LIMIT' "$file1" > "$file2"` passing `file1` and `file2` as variables (or using `$1` and `$2` positional parameters) You can redirect both ways, e.g. `grep 'DUE TO TIME LIMIT' < "$file1" > "$file2"` – David C. Rankin Feb 24 '16 at 17:16
  • @DavidC.Rankin: irrelevant - no, wait, creating a temporary file is actually worse. – Karoly Horvath Feb 24 '16 at 17:17
  • Then I don't understand the question... – David C. Rankin Feb 24 '16 at 17:17
  • clearly `$NEWEST` doesn't contain what you think it does. – Karoly Horvath Feb 24 '16 at 17:17
  • Then `varname=$(grep 'DUE TO TIME LIMIT' "$file1")` will work. – David C. Rankin Feb 24 '16 at 17:19
  • 1
    The full line that includes what you are grepping reads something like `slurmstepd: error: *** JOB 1234567 CANCELLED AT 2016-01-01T00:00:00 DUE TO TIME LIMIT on somenode001 ***` and those `*` are expanded when you run `echo $jobOver`, effectively listing the directory content. Running `echo "$jobOver"` instead will give you the expected result, as mentioned by @DavidC.Rankin. – damienfrancois Feb 24 '16 at 20:24
  • @damienfrancois and William Pursell, yes that was it. I didn't realize there were '*'s in the grep output and I didn't know they would be expanded! Thank you! – JohnND Feb 25 '16 at 08:41

1 Answers1

2

You can do the following:

jobOver=$(grep 'DUE TO TIME LIMIT' "$NEWEST")
echo "$jobOver"

Here, NEWEST is the variable containing the name of the file your want to grep.


Here is a local test I just ran:

$ cat slurm-3364.out
I DIED DUE TO TIME LIMIT, YESTERDAY.
I FEEL GREAT WHERE I AM.
YOU SHOULD JOIN ME.

$ NEWEST="slurm-3364.out"
$ jobOver=$(grep 'DUE TO TIME LIMIT' "$NEWEST")

$ echo "$jobOver"
I DIED DUE TO TIME LIMIT, YESTERDAY.
assefamaru
  • 2,751
  • 2
  • 10
  • 14