adding a word at the end of each line

Hi Folks,

I want to add a word after each line.

For eg

Line1
Line2
Line3
Line4

and output will be

Line1
test
Line2
test
Line3
test
Line4
test

Thanks in advance for any help
SK
surajkumar1 [ Di, 01 Januar 2008 15:56 ] [ ID #1896962 ]

Re: adding a word at the end of each line

surajkumar1 [at] gmail.com wrote:
>
> I want to add a word after each line.
>
> For eg
>
> Line1
> Line2
> Line3
> Line4
>
> and output will be
>
> Line1
> test
> Line2
> test
> Line3
> test
> Line4
> test

sed "/$/a\
test" filename

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos [ Di, 01 Januar 2008 16:09 ] [ ID #1896963 ]

Re: adding a word at the end of each line

surajkumar1 [at] gmail.com wrote:
>
> I want to add a word after each line.
>
> For eg
>
> Line1
> Line2
> Line3
> Line4
>
> and output will be
>
> Line1
> test
> Line2
> test
> Line3
> test
> Line4
> test

sed "/$/a\
test" filename

or with GNU sed:

sed "/$/atest" filename

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos [ Di, 01 Januar 2008 16:11 ] [ ID #1896964 ]

Re: adding a word at the end of each line

surajkumar1 [at] gmail.com wrote:
> Hi Folks,
>
> I want to add a word after each line.
>
> For eg
>
> Line1
> Line2
> Line3
> Line4
>
> and output will be
>
> Line1
> test
> Line2
> test
> Line3
> test
> Line4
> test
>
> Thanks in advance for any help
> SK

awk '{print $0 "\ntest"}' file

Ed.
Ed Morton [ Di, 01 Januar 2008 20:56 ] [ ID #1896969 ]

Re: adding a word at the end of each line

surajkumar1 [at] gmail.com wrote:
>
> I want to add a word after each line.
>
> For eg
>
> Line1
> Line2
> Line3
> Line4
>
> and output will be
>
> Line1
> test
> Line2
> test
> Line3
> test
> Line4
> test

$ echo "Line1
Line2
Line3
Line4" | perl -pe'$_ .= "test\n"'
Line1
test
Line2
test
Line3
test
Line4
test



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
someone [ Mi, 02 Januar 2008 01:36 ] [ ID #1897676 ]

Re: adding a word at the end of each line

surajkumar1 [at] gmail.com wrote:
>
> I want to add a word after each line.
>
> For eg
>
> Line1
> Line2
> Line3
> Line4
>
> and output will be
>
> Line1
> test
> Line2
> test
> Line3
> test
> Line4

With bash:

while read line; do echo -e "$line\ntest"; done < filename

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos [ Mi, 02 Januar 2008 01:49 ] [ ID #1897677 ]

Re: adding a word at the end of each line

On Jan 1, 7:56 pm, surajkum... [at] gmail.com wrote:
> Hi Folks,
>
> I want to add a word after each line.
>
> For eg
>
> Line1
> Line2
> Line3
> Line4
>
> and output will be
>
> Line1
> test
> Line2
> test
> Line3
> test
> Line4
> test
>



sed -e '
G
s/$/test/
' < yourfile
Rakesh Sharma [ Mi, 02 Januar 2008 09:02 ] [ ID #1897679 ]

Re: adding a word at the end of each line

On Jan 2, 1:02 pm, Rakesh Sharma <sharma... [at] hotmail.com> wrote:
> On Jan 1, 7:56 pm, surajkum... [at] gmail.com wrote:
>
>
>
> > Hi Folks,
>
> > I want to add a word after each line.
>
> > For eg
>
> > Line1
> > Line2
> > Line3
> > Line4
>
> > and output will be
>
> > Line1
> > test
> > Line2
> > test
> > Line3
> > test
> > Line4
> > test
>
> sed -e '
> G
> s/$/test/
> ' < yourfile


Hi,

What shhall we do if we want to add the same word at the beginning of
each line.


-Nirav
Nirav [ Do, 03 Januar 2008 13:09 ] [ ID #1898548 ]

Re: adding a word at the end of each line

On 2008-01-03, Nirav <niravshah84 [at] gmail.com> wrote:
>
>
> On Jan 2, 1:02 pm, Rakesh Sharma <sharma... [at] hotmail.com> wrote:
>> On Jan 1, 7:56 pm, surajkum... [at] gmail.com wrote:
>>
>> sed -e '
>> G
>> s/$/test/
>> ' < yourfile
>
>
> Hi,
>
> What shhall we do if we want to add the same word at the beginning of
> each line.
>
Change $ to ^.
Bill Marcum [ Do, 03 Januar 2008 14:41 ] [ ID #1898552 ]

Re: adding a word at the end of each line

On 1/3/2008 6:09 AM, Nirav wrote:
> On Jan 2, 1:02 pm, Rakesh Sharma <sharma... [at] hotmail.com> wrote:
>
>>On Jan 1, 7:56 pm, surajkum... [at] gmail.com wrote:
>>
>>
>>
>>
>>>Hi Folks,
>>
>>>I want to add a word after each line.
>>
>>>For eg
>>
>>>Line1
>>>Line2
>>>Line3
>>>Line4
>>
>>>and output will be
>>
>>>Line1
>>>test
>>>Line2
>>>test
>>>Line3
>>>test
>>>Line4
>>>test
>>
>>sed -e '
>> G
>> s/$/test/
>>' < yourfile
>
>
>
> Hi,
>
> What shhall we do if we want to add the same word at the beginning of
> each line.

after:

awk '{print $0 "\ntest"}' file

before:

awk '{print "test\n" $0}' file

at end:

awk '{print $0 "test"}' file

at beginning:

awk '{print "test" $0}' file

Regards,

Ed.
Ed Morton [ Do, 03 Januar 2008 15:06 ] [ ID #1898553 ]

Re: adding a word at the end of each line

On Jan 1, 10:56 pm, surajkum... [at] gmail.com wrote:
> Hi Folks,
>
> I want to add a word after each line.
>
> For eg
>
> Line1
> Line2
> Line3
> Line4
>
> and output will be
>
> Line1
> test
> Line2
> test
> Line3
> test
> Line4
> test
>
> Thanks in advance for any help
> SK

#!/bin/sh

while read line
do
echo $line
echo "TEST"
done < file
mik3l3374 [ Fr, 04 Januar 2008 07:39 ] [ ID #1899386 ]

Re: adding a word at the end of each line

On 1/4/2008 12:39 AM, mik3l3374 [at] gmail.com wrote:
> On Jan 1, 10:56 pm, surajkum... [at] gmail.com wrote:
>
>>Hi Folks,
>>
>>I want to add a word after each line.
>>
>>For eg
>>
>>Line1
>>Line2
>>Line3
>>Line4
>>
>>and output will be
>>
>>Line1
>>test
>>Line2
>>test
>>Line3
>>test
>>Line4
>>test
>>
>>Thanks in advance for any help
>>SK
>
>
> #!/bin/sh
>
> while read line
> do
> echo $line
> echo "TEST"
> done < file

ITYM:

while IFS= read -r line
do
echo "$line"
echo "TEST"
done < file

or even:

while IFS= read -r line
do
echo "$line\nTEST"
done < file

Regards,

Ed.
Ed Morton [ Fr, 04 Januar 2008 15:19 ] [ ID #1899397 ]

Re: adding a word at the end of each line

On Fri, 04 Jan 2008 08:19:32 -0600, Ed Morton wrote:
[...]
> while IFS= read -r line
> do
> echo "$line"

ITYM

printf '%s\n' "$line"

> echo "TEST"
> done < file
>
> or even:
>
> while IFS= read -r line
> do
> echo "$line\nTEST"

ITYM

printf '%s\nTEST\n' "$line"

> done < file
[...]

But running 2 commands per line is a very strange thing to do
especially when you can do the whole thing with only one
command.

--
Stephane
Stephane CHAZELAS [ Fr, 04 Januar 2008 15:33 ] [ ID #1899398 ]

Re: adding a word at the end of each line

> But running 2 commands per line is a very strange thing to do
> especially when you can do the whole thing with only one
> command.
>
> --
> Stephane


Its a perfectly legit way to do it. For the record, why use a while
loop when awk can do it? is it strange? :)
mik3l3374 [ Fr, 04 Januar 2008 16:01 ] [ ID #1899400 ]

Re: adding a word at the end of each line

On 1/4/2008 8:33 AM, Stephane Chazelas wrote:
> On Fri, 04 Jan 2008 08:19:32 -0600, Ed Morton wrote:
> [...]
>
>>while IFS= read -r line
>>do
>> echo "$line"
>
>
> ITYM
>
> printf '%s\n' "$line"

No. In the part you snipped:

>> #!/bin/sh
>>
>> while read line
>> do
>> echo $line
>> echo "TEST"
>> done < file


he's using /bin/sh with echo so his shell may not support printf while he's
obviously happy with echo. So, while printf may work, it's not what I meant.

Ed.
Ed Morton [ Fr, 04 Januar 2008 16:07 ] [ ID #1899403 ]

Re: adding a word at the end of each line

On Jan 4, 11:07 pm, Ed Morton <mor... [at] lsupcaemnt.com> wrote:
> On 1/4/2008 8:33 AM, Stephane Chazelas wrote:
>
> > On Fri, 04 Jan 2008 08:19:32 -0600, Ed Morton wrote:
> > [...]
>
> >>while IFS= read -r line
> >>do
> >> echo "$line"
>
> > ITYM
>
> > printf '%s\n' "$line"
>
> No. In the part you snipped:
>
> >> #!/bin/sh
>
> >> while read line
> >> do
> >> echo $line
> >> echo "TEST"
> >> done < file
>
> he's using /bin/sh with echo so his shell may not support printf while he's
> obviously happy with echo. So, while printf may work, it's not what I meant.
>
> Ed.

Sorry, I have been scripting in bourne shell since day one, although I
am learning bash now, old habits i guess. :). Last checked, my
Solaris box does have printf, so I guess it should work fine too.
cheers
mik3l3374 [ Fr, 04 Januar 2008 16:11 ] [ ID #1899404 ]

Re: adding a word at the end of each line

On Fri, 04 Jan 2008 09:07:43 -0600, Ed Morton wrote:
> On 1/4/2008 8:33 AM, Stephane Chazelas wrote:
>> On Fri, 04 Jan 2008 08:19:32 -0600, Ed Morton wrote:
>> [...]
>>
>>>while IFS= read -r line
>>>do
>>> echo "$line"
>>
>>
>> ITYM
>>
>> printf '%s\n' "$line"
>
> No. In the part you snipped:
>
>>> #!/bin/sh
>>>
>>> while read line
>>> do
>>> echo $line
>>> echo "TEST"
>>> done < file
>
>
> he's using /bin/sh with echo so his shell may not support printf while he's
> obviously happy with echo. So, while printf may work, it's not what I meant.
[...]

printf is a standard Unix command so should be in those Unices
where /bin/sh is still a
non-standard/deprecated/old-fashioned/you-name-it Bourne shell.

On the contrary, "read -r" is not Bourne (except for that
anecdotic Unix v8/SVR4.2 version)

--
Stephane
Stephane CHAZELAS [ Fr, 04 Januar 2008 16:27 ] [ ID #1899406 ]

Re: adding a word at the end of each line

On 1/4/2008 9:27 AM, Stephane Chazelas wrote:
> On Fri, 04 Jan 2008 09:07:43 -0600, Ed Morton wrote:
>
>>On 1/4/2008 8:33 AM, Stephane Chazelas wrote:
>>
>>>On Fri, 04 Jan 2008 08:19:32 -0600, Ed Morton wrote:
>>>[...]
>>>
>>>
>>>>while IFS= read -r line
>>>>do
>>>> echo "$line"
>>>
>>>
>>>ITYM
>>>
>>>printf '%s\n' "$line"
>>
>>No. In the part you snipped:
>>
>>
>>>>#!/bin/sh
>>>>
>>>>while read line
>>>>do
>>>> echo $line
>>>> echo "TEST"
>>>>done < file
>>>
>>
>>he's using /bin/sh with echo so his shell may not support printf while he's
>>obviously happy with echo. So, while printf may work, it's not what I meant.
>
> [...]
>
> printf is a standard Unix command so should be in those Unices
> where /bin/sh is still a
> non-standard/deprecated/old-fashioned/you-name-it Bourne shell.

Just because printf exists doesn't mean you can't be happy using echo.

> On the contrary, "read -r" is not Bourne (except for that
> anecdotic Unix v8/SVR4.2 version)
>

Good to know.

Ed.
Ed Morton [ Fr, 04 Januar 2008 16:33 ] [ ID #1899409 ]

Re: adding a word at the end of each line

On Fri, 04 Jan 2008 09:33:13 -0600, Ed Morton wrote:
[...]
>> printf is a standard Unix command so should be in those Unices
>> where /bin/sh is still a
>> non-standard/deprecated/old-fashioned/you-name-it Bourne shell.
>
> Just because printf exists doesn't mean you can't be happy using echo.
[...]

Sure, but you need to beware that echo is unreliable and
unportable. Especially, there's no guarantee of the behavior if
its arguments start with "-" or contain backslash characters.

So I think it's good practice to systematically replace it with
"printf" which doesn't have those issues.

--
Stephane
Stephane CHAZELAS [ Fr, 04 Januar 2008 16:51 ] [ ID #1899412 ]
Linux » comp.unix.shell » adding a word at the end of each line

Vorheriges Thema: Replace with sed command
Nächstes Thema: Req: Windows System Administrator / Domain Migration Specialist