-4

i have a scenario where i want to know in which position number the column name comes

like i have sample data below

ID|NAME|S1|BQ|S2|VN|D1
1|vimal|10.12|12.4|56.2|12.2|11.22
2|vilas|10.12|12.4|56.2|12.2|11.22
3|viky|10.12|12.4|56.2|12.2|11.22

Colum name : S1 S2 D1

output should be :

3
5
7
Kusalananda
  • 333,661

1 Answers1

1

Assuming the sample data is in file1:

awk -F"|" 'NR==1{for(i=1;i<=NF;i++){print $i"=$"i}}' file1 | grep -E "S1|S2|D1"

Using awk, we read only the 1st line and split into multiple columns where it is represented as col vs column number. Using grep, we are filtering only the list we are interested in.

Updated solution since OP changed the output format:

awk -F"|" 'NR==1{for(i=1;i<=NF;i++){if ($i ~ /S1|S2|D1/){print i;}}}' file
Guru
  • 5,905