0

I'am creating a shell script to extract a number from a particular line, where one particular string appears (isDone), for that i use a grep, i find the line and can echo it, but i can't store the grep output to a var. $text var got inumerous tags, one of them having the string "isDone", thats the line i want:

code:

short_str="isDone"
echo "$text" | grep "$short_str"

output:

< s:key name="isDone">1</s:key >

now i want to store that output from grep into a file, and then extract the value (on this case is 1)

what have i tried:

store="$("$text" | grep "$short_str")"
echo "$store"

but that outputs all the file, what am i doing wrong?

Bruno
  • 404
  • 4
  • 16
  • 1
    Can you provide sample input file? – Arkadiusz Drabczyk Feb 21 '20 at 16:02
  • 1
    You're at least missing the `echo` in `store="$("$text" | grep "$short_str")"`, but there's likely another problem as this mistake wouldn't usually lead to printing the whole file. I'm also confused how you can get an output of "1" in your first code when you're grepping a line that contains isDone, I would expect the output to contain isDone – Aaron Feb 21 '20 at 16:02
  • 1
    Make sure `$short_str` is still correctly defined, outputting the whole file could stem from having an actual filter of `grep ''` – Aaron Feb 21 '20 at 16:03
  • The duplicate https://stackoverflow.com/questions/4775548/how-to-pass-the-value-of-a-variable-to-the-stdin-of-a-command covers the need to use `echo` inside a pipeline, whereas https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash covers the more general question. BTW, consider both (1) running your code through http://shellcheck.net/, and (2) using `bash -x yourscript` to run the script with debug logging -- *before* asking questions here. – Charles Duffy Feb 21 '20 at 16:05
  • For starters, I would recommend `grep "$short_str" <<< $text` which is less error-prone than `echo "$text" | grep "$short_str"`. In order to store the grep result to a file, just call `grep "$short_str" > filename.txt <<< $text` where `filename.txt` is your file. To store the content of a file to a variable: `store=$(< filename.txt)` – vdavid Feb 21 '20 at 16:06
  • i can output what i want: < s:key name="isDone">1 but i cannot store it to a variable for me to extract the number of it!! sorry for my mistake – Bruno Feb 21 '20 at 16:11
  • 1
    If numbers only appear here in your line you could fetch it using `store=$(grep -o '[0-9]*' filename.txt)`. If you don’t want to use an intermediate file `filename.txt`, just get it like that: `store=$(grep "$short_str" <<< $text | grep -o '[0-9]*')` – vdavid Feb 21 '20 at 16:19
  • My file has a lot of numbers, i can only get the number when < s:key name="isDone">1 appear.. i can echo the line i want with echo "$text" | grep "$short_str" but i cannot store it into a val, so then i can extract the number of it.. @vdavid – Bruno Feb 21 '20 at 16:26
  • 1
    Ok i got it! store=$(grep "$short_str" <<< "$text" | grep "$short_str" | grep -o '[0-9]*') echo "$store" that will give me the number on that the line that has a string "isDone".. thank you all – Bruno Feb 21 '20 at 16:37

0 Answers0