adding a word at the end of each line

adding a word at the end of each line

am 01.01.2008 15:56:46 von surajkumar1

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

Re: adding a word at the end of each line

am 01.01.2008 16:09:19 von Cyrus Kriticos

surajkumar1@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.

Re: adding a word at the end of each line

am 01.01.2008 16:11:00 von Cyrus Kriticos

surajkumar1@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.

Re: adding a word at the end of each line

am 01.01.2008 20:56:32 von Ed Morton

surajkumar1@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.

Re: adding a word at the end of each line

am 02.01.2008 01:36:18 von someone

surajkumar1@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

Re: adding a word at the end of each line

am 02.01.2008 01:49:44 von Cyrus Kriticos

surajkumar1@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.

Re: adding a word at the end of each line

am 02.01.2008 09:02:56 von Rakesh Sharma

On Jan 1, 7:56 pm, surajkum...@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

Re: adding a word at the end of each line

am 03.01.2008 13:09:21 von Nirav

On Jan 2, 1:02 pm, Rakesh Sharma wrote:
> On Jan 1, 7:56 pm, surajkum...@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

Re: adding a word at the end of each line

am 03.01.2008 14:41:59 von Bill Marcum

On 2008-01-03, Nirav wrote:
>
>
> On Jan 2, 1:02 pm, Rakesh Sharma wrote:
>> On Jan 1, 7:56 pm, surajkum...@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 ^.

Re: adding a word at the end of each line

am 03.01.2008 15:06:17 von Ed Morton

On 1/3/2008 6:09 AM, Nirav wrote:
> On Jan 2, 1:02 pm, Rakesh Sharma wrote:
>
>>On Jan 1, 7:56 pm, surajkum...@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.

Re: adding a word at the end of each line

am 04.01.2008 07:39:09 von mik3l3374

On Jan 1, 10:56 pm, surajkum...@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

Re: adding a word at the end of each line

am 04.01.2008 15:19:32 von Ed Morton

On 1/4/2008 12:39 AM, mik3l3374@gmail.com wrote:
> On Jan 1, 10:56 pm, surajkum...@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.

Re: adding a word at the end of each line

am 04.01.2008 15:33:39 von Stephane CHAZELAS

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

Re: adding a word at the end of each line

am 04.01.2008 16:01:54 von mik3l3374

> 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? :)

Re: adding a word at the end of each line

am 04.01.2008 16:07:43 von Ed Morton

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.

Re: adding a word at the end of each line

am 04.01.2008 16:11:23 von mik3l3374

On Jan 4, 11:07 pm, 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.
>
> 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

Re: adding a word at the end of each line

am 04.01.2008 16:27:37 von Stephane CHAZELAS

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

Re: adding a word at the end of each line

am 04.01.2008 16:33:13 von Ed Morton

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.

Re: adding a word at the end of each line

am 04.01.2008 16:51:59 von Stephane CHAZELAS

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