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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search by fixed location... 2

Status
Not open for further replies.

bondtrails

Technical User
Nov 19, 2002
31
US
Hi everyone,
I am a relative newbie and have diligently been reading up on grep, sed and awk (and a few other unix goodies).

It seems that searching and extracting is done mainly by pattern matching. But what if I want to do a search on some fixed locations within a file.

For example: If i want to extract all records of a file that contain "Detail" in locations 57-62 of the record. Or how to insert commas (,) at locations 12, 18,22. Or how to search for records that contain "HiLife" in non-consecutive locations 25,26,29,33,34,35.

How to do??

--Bondster!!
 
sed '57,62\!d;/Detail/\!d;62q' input
This deletes lines not in range and not containing string 'Detail'. It also quits at line 62 to save on processing.

sed 's/.\{11\}/&,/;s/.\{17\}/&,/;s/.\{21\}/&,/' input
This insert commas at locations 12, 18 and 22 for all lines in input.

sed -n '25,26p;29p;33,35p' input | grep HiLife
or
awk 'BEGIN{LINES="(25|26|29|33|35)"} NR~LINES && /HiLife/{print}' input

The sed choice turns off output and expressly prints the desired lines. Grep then retains the lines with the string 'HiLife'. Awk works well too. This examples uses a list of lines to look at. If the NR (row number) contains (~) any of the lines and they contain the string 'HiLife' then print the line. /HiLife/ is a regular expression that is hoped to be found in the line. && means AND. /HiLife/ is working shorthand for $0 ~ /HiLife/. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
print all lines with 'Detail' starting at the 57th character in the line.
Code:
awk '{if (index($0,"Detail")==57) print}' input
or
Code:
sed '/.\{56\}Detail/\!d' input
for a sed version.

bigoldbulldogs sed for putting commas in works well :)

to get all lines with HiLife spaced out non-consecutively
Code:
sed '/.\{24\}Hi.\{2\}L.\{2\}ife/\!d' input

the awk equivalent is harder ... and 'm sure there is prettier ways of doing this.
Code:
awk '{SSTR=substr($0,25,2)""substr($0,29,1)""substr($0,33,3); if (SSTR == 'HiLife') print}' input

 
bigoldbulldog: your sed one-line are fantastic!! my star.
you my be should start a bigoldbulldog_sed_forum
... i will subscrive :) -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
however his first sed chooses lines 57 to 62 and greps those lines for 'Detail', rather than check every line for 'Detail' at positions 57 to 62.

and his third sed command similarly greps for 'HiLife' on a per line basis for lines 25,26,29,33,34,35, rather than split over non-consecutive characters on the same line.

maybe i got confused though.

which version did you want?
 
jad -

your right he said record, not recordset. We'll have to wait and what is really wanted. Both, ie lots of samples, is probably what he/she really wants. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Hey everyone,
thanks for the valuable assistance--I am learning something new today!
Just to clarify, I want to search the entire file (all records), but at pre-determined column locations.

So in my first request, I want to search an entire file (i.e. search every single record) for the text "Detail", but only in column locations 57-62.

Same is true for my other requests, I want to search (or insert) at given column locations for all records.

Thanks!!

--Bondster!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top