BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

grepping a specific column ???

SOLVED
Go to Solution
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
Honored Contributor

Re: grepping a specific column ???

Try this:

cut -c5-10 filename1 | grep findthis

Honored Contributor

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

Re: grepping a specific column ???

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

Re: grepping a specific column ???

Hi

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

Manoj Srivastava
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...
Honored Contributor

Re: grepping a specific column ???


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

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