Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the column number in a text file using unix commands

Suppose I have a text file with 6 columns as below

a|b|c|d|e|f

I know that somwhere in the file the character 'd' exist, but I want to know the column no for it

I have used the following command

awk 's=index($0,"d"){print "position="s}' filename

but it counts the delimiters too which I dont want....I want the output to be 4 in case of "d"

like image 900
user3622752 Avatar asked Dec 15 '22 23:12

user3622752


1 Answers

define "|" as Record Seperator, and use NR variable in awk:

awk -v RS="|" '/^d$/{print NR;}' filename

change ^d$ to whatever you want to match.

like image 154
yejinxin Avatar answered Jan 13 '23 13:01

yejinxin