Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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
Highlighted
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