문제

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"

도움이 되었습니까?

해결책

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.

다른 팁

Using awk you can do:

awk -v val='d' -F '|' '{for (i=1; i<=NF; i++) if ($i==val) {print i} }' file
4
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top