Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find a column number

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

I have a row with thousands of columns. Hot to find in which column# the specific string is present?

1) one for finding columns with exact string
2) one for finding columns with any occurence of the string (*string*)

thank you in advance.
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
maybe:

for i in `awk '{print $1}' file_with_very_long_line`;do echo $i;done|nl|grep string

 
sorry

for i in `cat file_with_very_long_line`;do echo $i;done|nl|grep string
 
what about this ?
awk '/string/{for(i=1;i<=NF;++i)if($i~/string/)print NR,i,$i}' file_with_very_long_line

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Another way (assuming the fields are space delimited)...
Code:
#!/bin/ksh

MATCH=string
FILE=file_with_very_long_line
ROW=0
unset IFS

while read LINE
do
    (( ROW += 1 ))
    COL=$(print "${LINE}" | sed "s/${MATCH}.*/${MATCH}/g"|wc -w)

    if (( COL ))
    then
        print "${MATCH} was found on row ${ROW} at column ${COL}"
    fi
done < ${FILE}
(untested)
 
How are the columns delimited ?
Assuming single space as separator
1) one for finding columns with exact string
awk 'BEGIN{NR=" "}/^string$/{print NR,string}' /path/to/bigline

2) one for finding columns with any occurence of the string (*string*)
awk 'BEGIN{NR=" "}/string/{print NR,$0}' /path/to/bigline

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top