4

Hi I have the following bash script code

group2=0
    while read -r line
    do
      popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c
        if [[ $popAll = 0 ]]; then
          group2 = $((group2+2)); 
        else
          group2 = $((group2+popAll+1));
        fi
    done << (grep -w "token" "$file")

and I get the following error:

./parsingTrace: line 153: syntax error near unexpected token `('
./parsingTrace: line 153: `done << (grep -w "pop" "$file")'

I do not want to pipe grep to the while, because I want variable inside the loop to be visible outside

codeforester
  • 39,467
  • 16
  • 112
  • 140
Kyriakos
  • 757
  • 8
  • 23
  • works if you move the pipe to front of while : `grep -w "token" "$file" | `... – amdixon Aug 07 '15 at 13:31
  • 1
    @amdixon but then you are running everything in a subshell, which may cause problems. – fedorqui Aug 07 '15 at 13:31
  • remove the space `done << (grep -w "token" "$file")` between `<< (` – luoluo Aug 07 '15 at 13:31
  • What does the desired line look like? There are far more efficient ways to get the length of the substring you want than that giant pipeline. – chepner Aug 07 '15 at 13:43
  • Thank you all for your comments. @chepner: Each line in the file that has the word "token" in, has the format "token {a,b,...}". I want to get his line and count the number of elements in each {} for each one. So I count the number of commas essentially – Kyriakos Aug 07 '15 at 13:51
  • `[[ $line =~ token\ {(.*)}]]; commas=${BASH_REMATCH[1]//[^,]}; popall=$((1+${#commas}))`. – chepner Aug 07 '15 at 13:57
  • Thank you @chepner. I will try this out! – Kyriakos Aug 07 '15 at 14:03
  • @luoluo no, this is not the issue here. – fedorqui Aug 07 '15 at 14:06

1 Answers1

8

The problem is in this line:

done << (grep -w "token" "$file")
#    ^^

You need to say < and then <(). The first one is to indicate the input for the while loop and the second one for the process substitution:

done < <(grep -w "token" "$file")
#    ^ ^

Note however that there are many others things you want to check. See the comments for a discussion and paste the code in ShellCheck for more details. Also, by indicating some sample input and desired output I am sure we can find a better way to do this.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Hi, thank you for the answer. This worked. Can you please explain me a bit the theory behind this? Also I am now getting: "./parsingTrace: line 151: group2: command not found", line 151 is "group2 = $((group2+popAll+1));" – Kyriakos Aug 07 '15 at 13:32
  • Ths is because of the line `popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c`, which is wrong. You need to indicate what you want to do here and give some examples. – fedorqui Aug 07 '15 at 13:34
  • Each line in the file that has the word "token" in, has the format "token {a,b,...}". I want to get his line and count the number of elements in each {} for each one. So I count the number of commas essentially – Kyriakos Aug 07 '15 at 13:36
  • @KKK Then you probably want to say `popAll=$(echo "$line| ... )` instead. Also, you are saying `if [[ $popAll = 0 ]]`, but for integers we need `-eq` to compare: `if [[ "$popAll" -eq 0 ]]` ... – fedorqui Aug 07 '15 at 13:38
  • Now I am getting the error on line 149 "group2 = $((group2+2));", "./parsingTrace: line 149: group2: command not found" – Kyriakos Aug 07 '15 at 13:42
  • @KKK because variable assignments cannot have spaces around the `=` use instead: `group2=$((group2+2))` – fedorqui Aug 07 '15 at 13:44
  • 1
    Thank you a lot...you solved all my problems. I am newbie to advance bash scripting. Any good ways to speedup knowledge? – Kyriakos Aug 07 '15 at 13:50
  • @KKK keep trying and keep asking : ) Read http://wiki.bash-hackers.org/doku.php and also reading questions/answers here in [SO] is very useful. Happy programming! – fedorqui Aug 07 '15 at 13:51