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

Getline

Status
Not open for further replies.

unmaxpine

Technical User
Jul 26, 2007
14
CH
Hello,

I would like to print the line -1 and the currentline in awk (by looking the * in a report) but I dont'know how to do:

I tried:
Code:
awk "/\*/{for(i=0;i<=1;i++){print;getline}}"
By this way, I have the current line and the next one.

I tried with ex too:
Code:
>File_out
ex File<<!
>g/\*/.-1,.+1w>>File_out
>q
>!
But by this way, I dont have the currentline.

Does anyone have an idea ?
 
What about this ?
awk '/\*/{print x;print}{x=$0}' File > File_out

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks works fine !

Coud you explain me the print x ?

Thanks
 
The first program part runs if a '*' is found: [red]print x[/red] : prints saved last line (saved in variable x) and [red]print[/red] : prints current line.

The second program part runs on every line: [red]x=$0[/red] : just saves the line ($0) in variable x so that it can be used in the first program part should the next line contain a '*'.


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top