PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > adding a word at the end of each line
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

adding a word at the end of each line

Réponse
 
LinkBack Outils de la discussion
Vieux 01/01/2008, 14h56   #1
surajkumar1@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
SK
  Réponse avec citation
Vieux 01/01/2008, 15h09   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

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.
  Réponse avec citation
Vieux 01/01/2008, 15h11   #3
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

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.
  Réponse avec citation
Vieux 01/01/2008, 19h56   #4
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

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
> SK


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

Ed.
  Réponse avec citation
Vieux 02/01/2008, 00h36   #5
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

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
  Réponse avec citation
Vieux 02/01/2008, 00h49   #6
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

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.
  Réponse avec citation
Vieux 02/01/2008, 08h02   #7
Rakesh Sharma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

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
  Réponse avec citation
Vieux 03/01/2008, 12h09   #8
Nirav
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

On Jan 2, 1:02 pm, Rakesh Sharma <sharma...@hotmail.com> 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

  Réponse avec citation
Vieux 03/01/2008, 13h41   #9
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

On 2008-01-03, Nirav <niravshah84@gmail.com> wrote:
>
>
> On Jan 2, 1:02 pm, Rakesh Sharma <sharma...@hotmail.com> 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 ^.

  Réponse avec citation
Vieux 03/01/2008, 14h06   #10
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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...@hotmail.com> 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.



  Réponse avec citation
Vieux 04/01/2008, 06h39   #11
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

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
> SK


#!/bin/sh

while read line
do
echo $line
echo "TEST"
done < file
  Réponse avec citation
Vieux 04/01/2008, 14h19   #12
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line



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

  Réponse avec citation
Vieux 04/01/2008, 14h33   #13
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 04/01/2008, 15h01   #14
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?
  Réponse avec citation
Vieux 04/01/2008, 15h07   #15
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 04/01/2008, 15h11   #16
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: adding a word at the end of each line

On Jan 4, 11:07 pm, Ed Morton <mor...@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
  Réponse avec citation
Vieux 04/01/2008, 15h27   #17
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 04/01/2008, 15h33   #18
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 04/01/2008, 15h51   #19
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h36.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,29064 seconds with 27 queries