Column number of matching string in column

Giles

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
jesse_b

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.

edited at
0

Comments

0 comments
Login to comment

Related

Count number of occurrences of a string in a cell range matching column and row headers

Column number of containing a string in column

complicated list column to column string matching and deriving another column

Formula to count the number of matching data in a column

R ID column matching row number

django: sort rows based on number of column matching

Trying to count the number of matching items in a table column

Get the number of levels in a column matching each level of another column

RegEx for matching and replacing only column-number-digit from a excel-formula string

Matching a exact word from a column to a string in another column and remove the matching word from string of other column

oracle insert string into number column

Split String and Number in Pandas Column

Number of consecutive digits in a column string

Check if string in one column is contained in string of another column in the same row and add new column with matching column name

Replace Matching Row String From Another Column

Matching rownames with a string contained within a column

Change column name with matching string from list

Python: String matching on a pandas column of lists

PySpark - String matching to create new column

Matching string in column value returns none value

filter a csv file by partial string matching for a column

Select column of dataframe with name matching a string in Julia?

Adding a column to a tibble using string matching

Character matching and replacing in a string column of dataframe

R: Extract matching string in dataframe column

Matching characters in text string in a column then invoke function

Dropping columns before a matching string in the following column

R - Rename all column titles right of matching string in column title

Insert a new column if value of previous column has some matching string