awk '{gsub("old","\n"); print}' adds additional new line

awk '{gsub("old","\n"); print}' adds additional new line

Post by Michael Wa » Sat, 03 May 2003 02:58:12



I want

1old2old

to become

--- cut here ---
1
2
--- cut here ---

Not

--- cut here ---
1
2

--- cut here ---

which awk '{gsub("old","\n"); print}' will give.

How to resolve the problem? Thanks.
--

 
 
 

awk '{gsub("old","\n"); print}' adds additional new line

Post by Barry Margoli » Sat, 03 May 2003 03:07:14




>I want

>1old2old

>to become

>--- cut here ---
>1
>2
>--- cut here ---

>Not

>--- cut here ---
>1
>2

>--- cut here ---

>which awk '{gsub("old","\n"); print}' will give.

>How to resolve the problem? Thanks.

The extra newline is because print prints a newline after printing the
line, but you also replaced "old" at the end of the line with a newline.
The solution is to delete a newline at the end of the line before printing:

awk '{gsub("old","\n"); sub("\n$", ""); print}'

--

Genuity Managed Services, a Level(3) Company, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

awk '{gsub("old","\n"); print}' adds additional new line

Post by Kevin Rodger » Sat, 03 May 2003 03:15:09



> I want

> 1old2old

> to become

> --- cut here ---
> 1
> 2
> --- cut here ---

> Not

> --- cut here ---
> 1
> 2

> --- cut here ---

> which awk '{gsub("old","\n"); print}' will give.

awk '{gsub("old","\n"); print}' | sed '$d'

awk '{gsub("old","\n"); printf("%s", $0)}'

--

 
 
 

awk '{gsub("old","\n"); print}' adds additional new line

Post by Stephane CHAZELA » Sat, 03 May 2003 04:40:37



> I want

> 1old2old

> to become

> --- cut here ---
> 1
> 2
> --- cut here ---

$ echo 1old2old | awk RT RS=old
1
2
$

(you might need GNU awk).

--
Stphane

 
 
 

awk '{gsub("old","\n"); print}' adds additional new line

Post by rakesh shar » Sat, 03 May 2003 11:34:42



> I want

> 1old2old

> to become

> --- cut here ---
> 1
> 2
> --- cut here ---

> Not

> --- cut here ---
> 1
> 2

> --- cut here ---

> which awk '{gsub("old","\n"); print}' will give.

> How to resolve the problem? Thanks.

in the bourne shell:

echo '1old2old' |
sed -e 's/old/\
/g  -e 's/\n$//'

in the c-shell:

echo '1old2old' |\
sed -e 's/old/\\
/g' -e 's/\n$//'

 
 
 

awk '{gsub("old","\n"); print}' adds additional new line

Post by Bill Marcu » Mon, 05 May 2003 08:59:53


On 1 May 2003 19:34:42 -0700, rakesh sharma


>> I want

>> 1old2old

>> to become

>> --- cut here ---
>> 1
>> 2
>> --- cut here ---

>> Not

>> --- cut here ---
>> 1
>> 2

>> --- cut here ---

>> which awk '{gsub("old","\n"); print}' will give.

>> How to resolve the problem? Thanks.

To be sure that the output ends with a newline, but only one newline:
awk '{gsub("old","\n");sub("\n*$",""); print}'

--
bill marcum the mushroom-eating laboratory monkey
What kind of monkey are you? http://thesurrealist.co.uk/monkey.cgi

 
 
 

1. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""

My machine: P166+mmx, 32mb ram, 4gb HD with Win95 and Win NT 4.0 and
redhat5.1 co-existed in different partitions.

I issued "sndconfig" within a xterm inside X Window, The program
detects the SB 16 pnp card sets it up with no error message, but when
it launch ModProbe to test out, it gets a message states: An error was
encountered running the ModProbe program."  I tried different IRQ
settings, all ends with the same message.

2nd, When I try to mount /dev/cdrom from File Systerm Manager, the
following error is returned:  Can't find /dev/hdb in /etc/mtab or
/etc/fstab.

3nd, When my machine boots, it halts at "Sendmail" for about 3-5 mins
before it goes to next step.  Obviously there isn't any mail system on
the machine right now because it is a standalone.  How can take this
mail thing out and speed up booting?

last one, Is my Zoom 56k PCI FaxModem a Windmodem that Linux can't use
to connect me to my local isp?

Experts help me out please. thanks.

2. linux startx

3. Type "(", ")" and "{", "}" in X...

4. Kernel memory hole on Linux 2.0.28

5. GETSERVBYNAME()????????????????????"""""""""""""

6. Web Ten - Mac Apache

7. Why doesn't echo "text" 'command' "more text" work?

8. Book Opinion

9. "Box", "line drawing", "special graphics" characters in aixterm

10. Does FreeBSD 3.0' ksh support "print" and "function" ?

11. WHY 'exec "$REALFILE" ${1+"$@"}'

12. "McCabe's Cyclomatic Complexity" and "Halstead's Metrics"