For Programmers: Free Programming Magazines  


Home > Archive > AWK > August 2007 > Combining lines and substituting characters









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Combining lines and substituting characters
molly2410@excite.com

2007-08-10, 6:57 pm

I need a script to combine lines and substitue a * where the old
carriage return was. I don't know the set amount of lines, for ex, the
text in the file is:

START 4050 08/07/06 Messages
This is a test

Approved.

START 4060 08/10/06 Messages
Test 2

Declined.

I want it to look like this:

START 4050 08/07/06 Messages*This is a test**Approved.
START 4060 08/10/06 Messages*Test 2**Declined.

Every valid new line begins with the word START.

Thanks,
Molly

Benjamin Esham

2007-08-10, 6:57 pm

molly2410@excite.com wrote:

> I need a script to combine lines and substitue a * where the old carriage
> return was. [...]


This will need a bit of work, as it prints an asterisk before each START
after the first one. However, it works otherwise:

/^Approved\.|^Declined\./ { print }
! /^Approved\.|^Declined\./ { printf $0 "*" }

HTH,
--
Benjamin D. Esham
E-mail/Jabber: bdesham@gmail.com | AIM bdesham128 | PGP D676BB9A
Ceci n'est pas une sig.
pop

2007-08-10, 6:57 pm

molly2410@excite.com said the following on 8/10/2007 8:55 AM:
> I need a script to combine lines and substitue a * where the old
> carriage return was. I don't know the set amount of lines, for ex, the
> text in the file is:
>
> START 4050 08/07/06 Messages
> This is a test
>
> Approved.
>
> START 4060 08/10/06 Messages
> Test 2
>
> Declined.
>
> I want it to look like this:
>
> START 4050 08/07/06 Messages*This is a test**Approved.
> START 4060 08/10/06 Messages*Test 2**Declined.
>
> Every valid new line begins with the word START.
>
> Thanks,
> Molly
>

brute fore method:
/^START/{if(lin!="") print lin;
lin=$0; ast="*"; next; }
(lin=="")||($1==""){next;}
{lin=lin""ast""$0; ast=ast"*";}
END{print lin}


--
(^\pop/^)
I Stopped to think but forgot to start again.
--
Ed Morton

2007-08-10, 6:57 pm

molly2410@excite.com wrote:

> I need a script to combine lines and substitue a * where the old
> carriage return was. I don't know the set amount of lines, for ex, the
> text in the file is:
>
> START 4050 08/07/06 Messages
> This is a test
>
> Approved.
>
> START 4060 08/10/06 Messages
> Test 2
>
> Declined.
>
> I want it to look like this:
>
> START 4050 08/07/06 Messages*This is a test**Approved.
> START 4060 08/10/06 Messages*Test 2**Declined.
>
> Every valid new line begins with the word START.
>
> Thanks,
> Molly
>


awk '!NF&&(++c==2){print o;o=s="";next}{o=o s $0;s="*"}END{print o}'

Ed.
Ed Morton

2007-08-10, 6:57 pm

Benjamin Esham wrote:

> molly2410@excite.com wrote:
>
>
>
>
> This will need a bit of work, as it prints an asterisk before each START
> after the first one. However, it works otherwise:
>
> /^Approved\.|^Declined\./ { print }
> ! /^Approved\.|^Declined\./ { printf $0 "*" }


The normal way of doing what you're trying to above would be:

/^Approved\.|^Declined\./ { print; next }
{ printf $0 "*" }

rather than testign the condition on one line then testing it's negative
onthe next. Also, ITYM:

.... { printf "%s*", $0 }

otherwise you'll get some very unexpected results if $0 contains a
printf formating sequence (e.g. "%d"). The first argument to printf must
always be a formatting string.

Regards,

Ed.
Benjamin Esham

2007-08-10, 6:57 pm

Ed Morton wrote:

> Benjamin Esham wrote:
>
>
> The normal way of doing what you're trying to above would be:
>
> /^Approved\.|^Declined\./ { print; next }
> { printf $0 "*" }
>
> rather than testing the condition on one line then testing its negative on
> the next.


OK... I had considered using the empty pattern for the "everything else"
match, but I didn't think of using next, so the line would have matched
everything.

> Also, ITYM:
>
> ... { printf "%s*", $0 }
>
> otherwise you'll get some very unexpected results if $0 contains a printf
> formating sequence (e.g. "%d"). The first argument to printf must always
> be a formatting string.


I don't use printf often—only when I need to suppress print's trailing
newline, actually—so I'm not that familiar with its use.

Thanks a lot for the tips!
--
Benjamin D. Esham
E-mail/Jabber: bdesham@gmail.com | AIM bdesham128 | PGP D676BB9A
"People who boast about their I.Q. are losers."
— Stephen Hawking
Kenny McCormack

2007-08-10, 6:57 pm

In article <kb0vi.13830$B25.1045@news01.roc.ny>,
Benjamin Esham <bdesham@gmail.com> wrote:
....
>I don't use printf often—only when I need to suppress print's trailing
>newline, actually—so I'm not that familiar with its use.


In that case, you might want to consider setting ORS="".
I do this frequently when I want to use multiple print statements to
generate a single line of output. It saves the hassle of dealing with
printf. At the end, do: print "\n" to get the newline.
molly2410@excite.com

2007-08-10, 6:57 pm

On Aug 10, 11:30 am, pop <p_...@MUNGEDhotmail.com> wrote:
> molly2...@excite.com said the following on 8/10/2007 8:55 AM:
>
>
>
>
>
>
>
>
>
>
>
>
> brute fore method:
> /^START/{if(lin!="") print lin;
> lin=$0; ast="*"; next; }
> (lin=="")||($1==""){next;}
> {lin=lin""ast""$0; ast=ast"*";}
> END{print lin}
>
> --
> (^\pop/^)
> I Stopped to think but forgot to start again.
> --- Hide quoted text -
>
> - Show quoted text -


Works perfect. Thanks for all the suggestions.

Scofield

2007-08-17, 3:58 am

Ed Morton 写道:
> molly2410@excite.com wrote:
>
>
> awk '!NF&&(++c==2){print o;o=s="";next}{o=o s $0;s="*"}END{print o}'
>
> Ed.

good job?
but i cannot explain how it works,what does "o" and "s" stand for
loki harfagr

2007-08-17, 7:57 am

On Fri, 17 Aug 2007 15:03:54 +0800, Scofield wrote:

> Ed Morton 写道:
> good job?


Why the question mark?-D) It *is* good job!

> but i cannot explain how it works,what does "o" and "s" stand for


Think it like this and you'll find out how:

c == count
s == separator (and/or sequel :-)
o == output (and/or original)

simmer slowly til ready...
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com