-1

My file Threadout.txt contains like Thread 1/1/25/100

val1=$(grep 'Thread' Threadout.txt | awk -F"/" '{print $1}')  
val2=$(grep 'Thread' Threadout.txt | awk -F"/" '{print $2}')    
val3=$(grep 'Thread' Threadout.txt | awk -F"/" '{print $3}')  
val4=$(grep 'Thread' Threadout.txt | awk -F"/" '{print $4}')

for((i=1;i<5;i++))  
do  
  val[i]=$(grep 'Thread' Threadout.txt | awk -F"/" '{print $[i]}')  
  echo $val[i] 
done

I have tried this array but not giving the correct result like 1 1 25 100 values. Any error?

3
  • Please give a complete example of the content of Threadout.txt. Does it really only contain the line Thread 1/1/25/100?
    – AdminBee
    Commented Jan 27, 2021 at 9:08
  • 4
    Please don't post the same question again, rather improve your first question to get answers there --> How to print the position. Especially I still don't understand what is your problem with my answer which seem to exactly do what you expect.
    – pLumo
    Commented Jan 27, 2021 at 9:12
  • @DastageerUmar Do you get any output or error when you run the script? If so, could you please add them to the question?
    – Umlin
    Commented Jan 27, 2021 at 9:19

1 Answer 1

4

There are several issues with your code.

  • The first important thing to remember is that since your awk program is enclosed in single quotes (which is recommended), no shell parameter expansions take place, and your statement print $[i] will not work. See this exhaustive Q&A on StackOverflow on how to use shell parameter in awk programs.
  • Even so, an awk field whose number is stored in a (awk) variable n would be addressed via $n, not $[n].
  • You assign the variables val1 to val4 but never actually use them, so the first part of your script example doesn't do anything.
  • Finally, to dereference an array variable, you have to state ${val[i]}, not $val[i] (which would print the content of the variable $val- the empty string - followed by the fixed string [i]).

There are also several inefficiencies such as the repeated call to awk and (as far as I can tell) a failure to address the issue of the leading Thread in your example input when using just / as field separator.

To address these issues, and since the relevant values do not contain whitespace in your example, try the following:

#!/bin/bash

val=( $(awk '/^Thread/{n=split($2,a,/\//); for (i=1;i<=n;i++) printf("%s%s",a[i],OFS)}' Threadout.txt) )

for i in ${!val[@]}
do
    echo "${val[i]}"
done
  • The awk call will split the second "field" of the line Thread 1/1/25/100, namely the 1/1/25/100 part at the / into the array a and print all entries of the array, separated by OFS (which defaults to space).
  • The shell script will take the output of that awk program and - since it is a space-separated list of tokens - assign the individual tokens to an array variable val via the val=( ... ) statement.
  • It will then iterate over the array indices and print the array values.

You may want to read into bash arrays a little further in the GNU Bash Reference Manual, or GreyCat&Lhunath's Bash Guide. I would also recommend you to take a look at shellcheck to debug your shell scripts.

6
  • 1
    you should post this on the original question instead of here.
    – pLumo
    Commented Jan 27, 2021 at 9:25
  • @pLumo Heh, you are right, I hadn't seen that one.
    – AdminBee
    Commented Jan 27, 2021 at 9:28
  • awk: syntax error near line 1 awk: illegal statement near line 1 Commented Jan 27, 2021 at 10:06
  • @DastageerUmar once again, please state which awk version you have installed.
    – AdminBee
    Commented Jan 27, 2021 at 10:14
  • @AdminBee Thanks a lot and my script is working fine.Now How to assign this each all 4 values into another variable from this echo ${val[i]}".??Because i need to use the each value into IF condition.Please help. Commented Jan 27, 2021 at 13:55

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