Re: grepping a specific column ??? (1413 Views)
Reply
Occasional Advisor
HP System Handle Owner
Posts: 29
Registered: ‎12-14-1999
Message 1 of 8 (1,413 Views)
Accepted Solution

grepping a specific column ???


Can anyone advise on a command or script which will read through a large file line by line extracting lines which contain a specific strings of characters which appear between certain places on a line.

e.g grep abcde filename1 > filename2 works, but I only want not look in characters 5-10 of each line and no where else on the line.

Many thanks
Not another questionnaire !!
Honored Contributor
John Poff
Posts: 2,448
Registered: ‎05-22-2001
Message 2 of 8 (1,413 Views)

Re: grepping a specific column ???

Try this:

cut -c5-10 filename1 | grep findthis

Honored Contributor
Kevin Wright
Posts: 760
Registered: ‎09-19-2000
Message 3 of 8 (1,413 Views)

Re: grepping a specific column ???

you need to use awk for this, if I understand you question correctly.
columns start at $0, $1, $2
so to print the second column
awk '{print $1}'
whitespace is the default deliminator, to change this use -F .
Honored Contributor
Vincenzo Restuccia
Posts: 1,353
Registered: ‎03-20-2001
Message 4 of 8 (1,413 Views)

Re: grepping a specific column ???

awk '{ print $5,$10 }'filename1|grep file_find
Honored Contributor
MANOJ SRIVASTAVA
Posts: 1,885
Registered: ‎07-10-2000
Message 5 of 8 (1,413 Views)

Re: grepping a specific column ???

Hi

try this
cat filename | awk '{ print $5 } | cut -c5-10

Manoj Srivastava
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 6 of 8 (1,413 Views)

Re: grepping a specific column ???

How about

awk 'substr($0,5,5)="datum"{print $0}' your_file

awk can compare a substring and then print only thos lines
or

perl -nie 'print if substr($_,4,5) eq "datum"'

perl can also compare a substring and then print
There be dragons...
Honored Contributor
Carlos Fernandez Riera
Posts: 1,172
Registered: ‎09-16-1998
Message 7 of 8 (1,413 Views)

Re: grepping a specific column ???


awk ' $2 == "data" { print $0 }' file.

unsupported
Esteemed Contributor
Bruce Regittko_1
Posts: 273
Registered: ‎11-15-2000
Message 8 of 8 (1,413 Views)

Re: grepping a specific column ???

Hi,

Try using a regular expression:

grep ".....abcde" file1 > file2

The . wil match any single character. You could probably get away with the following, which would guard against the wrong number of "dots":

grep ".\{5\}abcdef" file1 > file2

See the regexp(5) and the grep(1) man pages for more information.

--Bruce
www.stratech.com/training
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.