|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 ^. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
> 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? ![]() |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
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, mySolaris box does have printf, so I guess it should work fine too. cheers |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|