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

find text in file & cut into new file 1

Status
Not open for further replies.

JasonCooper

IS-IT--Management
Dec 14, 2001
20
0
0
GB
Hi,
I need to script the following:
find lines containing '5' in position 'x' and cut that line plus the next line into a new file.

Any help would be greatly appreciated
Many thanks in advance

Example file below:
10 451125158200601000400091210101 701 125.51
40 4511251582006 95039Please delete this
10 461126409200601000400091210501 701 138.22
40 4611264092006 95039Please delete this
10 391118262200601010400091210101 701 90.85
40 3911182622006 95039Please delete this
10 391123474200681020400091210101 701 90.59
40 3911234742006 95039Please delete this
10 451124973200681020400091210101 701 93.55
40 4511249732006 95039Please delete this
10 391123519200681020400091211201 701 98.78
40 3911235192006 95039Please delete this
10 341108109200681037400060210100 1004 46.18
40 3411081092006 Please delete this
10 341108113200681037400060210100 1004 14.30
40 3411081132006 Please delete this
10 351109759200651037400060210100 1004 11.54
40 3511097592006 Please delete this
10 351110074200651037400060210100 1004 14.30
40 3511100742006 Please delete this
10 351110076200651037400060210100 1004 23.09
40 3511100762006 Please delete this
10 351110077200651037400060210100 1004 11.54
40 3511100772006 Please delete this
10 351110131200651037400060210100 1004 23.09
40 3511101312006 Please delete this
10 351110166200651037400060210100 1004 14.30
40 3511101662006 Please delete this
10 351110260200651037400060210100 1004 12.53
40 3511102602006 Please delete this
 
thankyou for the quick response Feherke,

I've tried

awk '$17=="5"{print;getline;print}' inputfile >outputfile

but the outfile is empty, am i being really silly and missing something obvious ??!!


thankyou
 
Hi

Hmm... Wired. It works for me with [tt]gawk[/tt] on CygWin. In fact, I can not imagine anything that could go wrong in such short script.

Maybe the 17[sup]th[/sup] field has some other characters beside that "5" ?
Code:
awk '$17[red]~[/red]"5"{print;getline;print}' inputfile >outputfile
Or do you bean character position ?
Code:
awk '[red]substr($0,17,1)[/red]=="5"{print;getline;print}' inputfile >outputfile

Feherke.
 
Hi,

may I jump in here?
I think fehereke's awk considers each character as a field, whereas JasonCooper's (like mine) relies on the FS field separator for separating a line into fields.
fehereke, did you define any unusual defaults?

JasonCooper, this should work:
awk -v FIELDWIDTHS=1 '$17=="5"{print;getline;print}' inputfile >outputfile

regards
 
thats fantastic, the substr works a treat, thankyou.
how would i then go about deleting those lines identified by that awk from the original file ?

many thanks, you're a star
 
Hi

To delete those lines ? First I understood that you want to keep that and the next line. Ok, then to remove it :
Code:
awk 'substr($0,17,1)=="5"{next}1' inputfile >outputfile

Feherke.
 
Hi

hoinz said:
I think fehereke's awk considers each character as a field, whereas JasonCooper's (like mine) relies on the FS field separator for separating a line into fields.
fehereke, did you define any unusual defaults?
No, the [tt]gawk[/tt]'s default [tt]FS[/tt] is " " ( one space ). So it splits on one or more whitespace characters ( space, horizontal tab, newline ).

And yes, it is possible to set it to consider each character a separate field, by setting the [tt]FS[/tt] to "" ( null string ).

Feherke.
 
Feherke,

there was some misunderstanding; in the meantime I found out that the b in your Or do you bean character position ? probably should have been an m.
[wink]
 
the awk you suggested to delete the lines with "5" in character position 17 from the original file results in:

awk: syntax error near line 1
awk: bailing out near line 1

sorry, i have no experience of using awk, any suggestions ?

thankyou
 
Hi

That would be a syntax error. I copy&pasted back the code I posted above and still works. Please double check yours.

I do not think is possible, but if you have some strange version of [tt]awk[/tt], try this :
Code:
awk 'substr($0,17,1)=="5"{next}[red]{print}[/red]' inputfile >outputfile

Feherke.
 
we're getting there :)

i must have strange version of awk !!
that command deletes the lines containing '5' in position '17' but the next line(s) are simply moved to the end of the file rather than deleted !!??
 
Hi

Sorry, I misunderstood you. I thought the requirements changed and the deletion does not apply to the next line too.
Code:
awk 'substr($0,17,1)=="5"{[red]getline;[/red]next}{print}' inputfile >outputfile

Feherke.
 
fantastic, thankyou very much for your prompt responses
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top