Operating System - HP-UX
1748140 Members
3690 Online
108758 Solutions
New Discussion юеВ

Re: grepping a specific column ???

 
SOLVED
Go to solution
HP System Handle Owner
Occasional Advisor

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 !!
7 REPLIES 7
John Poff
Honored Contributor

Re: grepping a specific column ???

Try this:

cut -c5-10 filename1 | grep findthis

Kevin Wright
Honored Contributor
Solution

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 .
Vincenzo Restuccia
Honored Contributor

Re: grepping a specific column ???

awk '{ print $5,$10 }'filename1|grep file_find
MANOJ SRIVASTAVA
Honored Contributor

Re: grepping a specific column ???

Hi

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

Manoj Srivastava
Rodney Hills
Honored Contributor

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...
Carlos Fernandez Riera
Honored Contributor

Re: grepping a specific column ???


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

unsupported
Bruce Regittko_1
Esteemed Contributor

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