modify file using awk

I have a file, a.asc which is generated from a shell script:

-----BEGIN PGP MESSAGE-----
Version: PGP 6.5.8

qANQR1DBwE4DR5PN6zVjZTcQA/9z5Eg94cwYdTnC7v+JUegQuJwHripqnyjFrEs/ejzKYCNmngbHHmf8V4K3uFkYyp74aFf+CdymA030RKs6ewOwkmqRW19oIXCgVe8Qmfg+/2KTq8XN
=0QSP
-----END PGP MESSAGE-----

I want to remove the header, footer and the blank lines (---, Version etc;) and modify the same file. That is after I append my command in the script file, a.asc file should look like this (No blank lines):

qANQR1DBwE4DR5PN6zVjZTcQA/9z5Eg94cwYdTnC7v+JUegQuJwHripqnyjFrEs/ejzKYCNmngbHHmf8V4K3uFkYyp74aFf+CdymA030RKs6ewOwkmqRW19oIXCgVe8Q
mfg+/2KTq8XN
=0QSP

I used awk command to identify the header, footer and blank lines.
awk '$1 ~ /(^-----)|(^Version)|(^$)/' a.asc

How do I remove these lines from a.asc so that it is modified?
Any help would be great.

Thanx

remember the "!" .

Regards. Hugo.

Hi Hugo,

Couldnt make it work. I think I will have to use sed here.

Thanx
Nitin

man awk --> NR

if ( NR = 4 ) {print $0}

sed also is an excelent choice.

Regards, hugo

Thanks Hugo but I guess I wanted to know if I could modify the same file using these commands. In other words, I would like to delete the header and blank line from a.asc itself.

Your awk command finds the lines you do not want, so if you include the negative operator that hugo_perez mentioned, then you get all but those lines:

awk '$1 !~ /(^-----)|(^Version)|(^$)/' a.asc > a.ascNEW

But cannot modify a.asc directly. You will have to rename.

natty (if you have solarias) see and read the output of:

man 5 regexp

Thanx Hugo and Jimbo. I think I will have to use and a.ascNEW approach here.

grep is what you are looking for.

First use ^$ to remove any blank lines, then grep -v to remove unique lines in the file. Then redirect to a new filename.

cat filename |grep -v ^$ |grep -v "unique header" |grep -v "unique footer" > new.filename

Remember the -v option for grep will remove/ignore lines and send the rest to standard out.

:cool: :wink:

I think the one awk process is a far superior solution than four processes piped together.

Is superior, in all the ways. If somebody doubts it, use the time command.

yea, yea, yea...:rolleyes: :o

I know that it wastes some amount of time and is firing off multiple processes...

I was going for simplicity. However, for single use items like removing blank lines grep -v ^$ is easy to remember.

I enjoy using awk as well, it is very cool. I am making my way through "The AWK Programming Language by Aho, Kerningan, Weinberger". Great book.

:cool:

Thanks, Kelum_Magnus, for the awk book reference. To me, awk and vi were both very frustrating until I got the hang of them, now I love them.

And I definitely am not bashing grep. Every day you can see me using 4-5 piped greps.

But I strive for best practice. and I have improved my unix a huge amount, thanks to this forum and all the contributors, yourself included.