I know this seems very simple but I can't get it to work or find a functional option.
Linux UBUNTU OS, bash script
I need to Identify a column number based on a string (there will always be a varying number of columns to search through)
Example input:
STRING="Boo"
Hi Boo Jiggy
Example Output:
2
Using awk:
$ string=Boo
$ echo 'Hi Boo Jiggy' | awk -v b="$string" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}'
2
We pass your search string as an awk parameter and then loop through each field in the input. If the current field is equal to the search parameter we print it's number.
Note: this will find more than one string if they exist but will not work for substrings.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments