Google Answers Logo
View Question
 
Q: Awk Question in FreeBSD ( No Answer,   3 Comments )
Question  
Subject: Awk Question in FreeBSD
Category: Computers > Programming
Asked by: samuelnorris-ga
List Price: $2.00
Posted: 23 Sep 2003 16:33 PDT
Expires: 24 Sep 2003 10:37 PDT
Question ID: 259568
Awk script - need to append something to the end of a line if one
doesn't already exist.

Example:

DOMAIN TYPE RECORD
DOMAI1 TYPE RECORD.ONE.
DOMAI2 TYPE RECORD.TWO.
DOMAI3 TYPE RECORD
DOMAI4 TYPE RECORD.THREE

needs to become:

DOMAIN TYPE RECORD.NEW.
DOMAI1 TYPE RECORD.ONE.
DOMAI2 TYPE RECORD.TWO.
DOMAI3 TYPE RECORD.NEW.
DOMAI4 TYPE RECORD.THREE

Basically appending .NEW. to the end of each line that does not end in
a period and a newline char.

I'd like to get this done with an AWK command - and I am using files
that are 1-2gb a piece.

Clarification of Question by samuelnorris-ga on 23 Sep 2003 23:14 PDT
Yes, I did mistype the last line. . .  It should have ended up with a
.NEW. as well.

Thanks - I think that did it, I just couldn't get the syntax right on
the replace command.  Here is what I ended up with:

sed -e 's/[^\.]$/.NET./g' -e 's/ NS /.NET NS /g' net.dat | awk
'{fname="net." substr($0,1,1) ".dat"; print $0 > fname}'

I used sed for the replacement ( 2 replacements per line usually ) and
then awk to split the file into many files.  The data file is 1gb in
size and to perform tasks its easier to split it by the first letter
of each line.

Awesome, thanks!
Answer  
There is no answer at this time.

Comments  
Subject: Re: Awk Question in FreeBSD
From: eadfrith-ga on 23 Sep 2003 22:07 PDT
 
[Your example doesn't follow your rule. By your rule the final line
ending in .THREE should have a .NEW. appended. I'm assuming your rule
is correct and you missed the trailing period in the example.]

Anyway, I'm no AWK expert, but at least on my WinXP machine the
following program works:

BEGIN {}
/\.$/    {print $0;}          # match lines ending in a period
/[^\.]$/ {print $0".NEW.";}   # match lines that don't end in a period
END {}

If you save this in a file called append.awk, and your data is in
records.dat, then you'd run the following command:

> awk -f append.awk records.dat > new_records.dat

I found the following AWK tutorials to be quite helpful:

http://www.canberra.edu.au/~sam/whp/awk-guide.html

http://www.vectorsite.net/tsawk1.html

http://www.gnu.org/manual/gawk-3.1.1/html_node/index.html

Once you've grasped the basic structure of an AWK program it all comes
down to learning how to construct the correct regular expression,
which can be often be challenging.

Hope this helps.

Cheers,

Eadfrith
Subject: Re: Awk Question in FreeBSD
From: samuelnorris-ga on 23 Sep 2003 23:14 PDT
 
so how does this work - can you put that reply in as an answer and
then it will pay you the money?  I'd like to give it to you since you
really did solve my question.

Thanks,
Sam
Subject: Re: Awk Question in FreeBSD
From: eadfrith-ga on 24 Sep 2003 09:59 PDT
 
Well, I'm not a researcher, so this one's on the house.

Glad you got the solution you wanted.

Cheers,

Eadfrith

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy