Operating System - HP-UX
1838656 Members
3279 Online
110128 Solutions
New Discussion

sed/awk search and delete lines

 
SOLVED
Go to solution
Lisa  Mauer
Regular Advisor

sed/awk search and delete lines

I have a programmer request to search through files (cobol compiled listings), search the entire file for a string "LAYOUT", once that string is found, copy from and including the string "LAYOUT" to the end of file and put the output to a new file. Not being a sed expert I thought I would post here to see if anyone could help. Thanks!
9 REPLIES 9
Pete Randall
Outstanding Contributor
Solution

Re: sed/awk search and delete lines

Courtesy of Princess Paula's handy SED one liner's (see attached):

# print section of file from regular expression to end of file
sed -n '/regexp/,$p'

Pete
H.Merijn Brand (procura
Honored Contributor

Re: sed/awk search and delete lines

# perl -ne 'BEGIN{$/=undef}<>=~s/.*?(?=LAYOUT)//s&&print' file >newfile
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: sed/awk search and delete lines

Pete, correct - of course - but if you take it literally, this will not do what it should if "LAYOUT" is halfway the line


Blah LAYOUT blah

will leave the first Blah plus space in place where the case wanted "copy from and including the string" which should IMHO strip everything in front

# perl -ne '/LAYOUT/..undef and print' does the same as your (Paula's) sed command
Enjoy, Have FUN! H.Merijn
Pete Randall
Outstanding Contributor

Re: sed/awk search and delete lines

Merijn,

I, of course, defer to your superior Perl knowledge.

;^)

Pete

Pete
Lisa  Mauer
Regular Advisor

Re: sed/awk search and delete lines

Ok... guess I am just a perl dunce, but when I run it I get this error:
Can't modify in substitution at -e line 1, near "s/.*?(?=LAYOUT)//s&&"
Execution of -e aborted due to compilation errors.

Sorry ;(
Robin Wakefield
Honored Contributor

Re: sed/awk search and delete lines

Hi Lisa,

Try:

perl -ne 'BEGIN{$/=undef}s/.*?(?=LAYOUT)//s&&print' file > newfile

Rgds, Robin
H.Merijn Brand (procura
Honored Contributor

Re: sed/awk search and delete lines

Thanks for the correction! At least it proves either that I'm not perfect (indeed I am not) and/or I am tired (indeed I am)

Don't ever take every solution I give for granted :)
Enjoy, Have FUN! H.Merijn
Ceesjan van Hattum
Esteemed Contributor

Re: sed/awk search and delete lines

Assuming that a line begins with LAYOUT, like

inputfile:
abc adf
abc asdf
LAYOUT
adf asdf
asdf asdf


awk '{if ($1=="LAYOUT") {
start_print=1 }
if (start_print==1){
print $0 }
}' inputfile

Regards,
Ceesjan


Lisa  Mauer
Regular Advisor

Re: sed/awk search and delete lines

Worked like a charm!!! As always - Thank you very much!!!!