Adding prefix and suffix

  • Follow


Hi,

How to add a prefix and suffix to each line of a text file using unix
shell scripting?

Input file : Input.txt with contents

1
2
3

Expected output:

'1',
'2',
'3',

Regards,
DI
-1
Reply deepan.17 (16) 7/23/2010 9:18:08 AM

On 23/07/2010 11.18, Javas wrote:
[...]
> How to add a prefix and suffix to each line of a text file using unix
> shell scripting?
>
> Input file : Input.txt with contents
>
> 1
> 2
> 3
>
> Expected output:
>
> '1',
> '2',
> '3',


sed "s/.*/'&',/" Input.txt


Regards
Dimitre
0
Reply Radoulov 7/23/2010 9:28:24 AM


On Jul 23, 2:28=A0pm, "Radoulov, Dimitre" <cichomit...@gmail.com> wrote:
> On 23/07/2010 11.18, Javas wrote:
> [...]
>
> > How to add a prefix and suffix to each line of a text file using unix
> > shell scripting?
>
> > Input file : Input.txt with contents
>
> > 1
> > 2
> > 3
>
> > Expected output:
>
> > '1',
> > '2',
> > '3',
>
> sed "s/.*/'&',/" Input.txt
>
> Regards
> Dimitre

Thank You Mr.Dimitre.
0
Reply Javas 7/23/2010 9:30:51 AM

I would say: sed -i "s/.*/'&',/" input.txt
0
Reply hehe2046 (7) 7/23/2010 1:11:02 PM

In article <MP6dneh2BdV5CdTRnZ2dnUVZ_sydnZ2d@giganews.com>,
hehe2046  <user@compgroups.net/> wrote:
>I would say: sed -i "s/.*/'&',/" input.txt
>
>

Or, more sensibly:

awk '{print "\047"$0"\047"}' ...

Tip: Avoid cryptic, meaningless tools like "sed" (and friends).

-- 
> No, I haven't, that's why I'm asking questions. If you won't help me,
> why don't you just go find your lost manhood elsewhere.

CLC in a nutshell.

0
Reply gazelle 7/23/2010 1:26:44 PM

Kenny McCormack wrote:

> In article <MP6dneh2BdV5CdTRnZ2dnUVZ_sydnZ2d@giganews.com>,
> hehe2046  <user@compgroups.net/> wrote:
>>I would say: sed -i "s/.*/'&',/" input.txt
>>
>>
> 
> Or, more sensibly:
> 
> awk '{print "\047"$0"\047"}' ...

You missed the comma, by the way.
> 
> Tip: Avoid cryptic, meaningless tools like "sed" (and friends).
> 

How is 'print "\047"$0"\047" less cryptic than the above sed line?

Sorry, I don't want to start an awk V sed flame war, but let's face it, that
so-called tip IS flame-bait.

Andrew
0
Reply Andrew 7/23/2010 4:15:58 PM

In article <i2cf5j$982$1@south.jnrs.ja.net>,
 Andrew McDermott <a.p.mcdermott@NOSPAM-rl.ac.uk> wrote:

> Kenny McCormack wrote:
> 
> > In article <MP6dneh2BdV5CdTRnZ2dnUVZ_sydnZ2d@giganews.com>,
> > hehe2046  <user@compgroups.net/> wrote:
> >>I would say: sed -i "s/.*/'&',/" input.txt
> >>
> >>
> > 
> > Or, more sensibly:
> > 
> > awk '{print "\047"$0"\047"}' ...
> 
> You missed the comma, by the way.
> > 
> > Tip: Avoid cryptic, meaningless tools like "sed" (and friends).
> > 
> 
> How is 'print "\047"$0"\047" less cryptic than the above sed line?
> 
> Sorry, I don't want to start an awk V sed flame war, but let's face it, that
> so-called tip IS flame-bait.
> 
> Andrew

I think he was attempting a joke.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Reply Barry 7/23/2010 6:03:41 PM

Fri, 23 Jul 2010 14:03:41 -0400, Barry Margolin did cat :

> In article <i2cf5j$982$1@south.jnrs.ja.net>,
>  Andrew McDermott <a.p.mcdermott@NOSPAM-rl.ac.uk> wrote:
> 
>> Kenny McCormack wrote:
>> 
>> > In article <MP6dneh2BdV5CdTRnZ2dnUVZ_sydnZ2d@giganews.com>, hehe2046 
>> > <user@compgroups.net/> wrote:
>> >>I would say: sed -i "s/.*/'&',/" input.txt
>> >>
>> >>
>> >>
>> > Or, more sensibly:
>> > 
>> > awk '{print "\047"$0"\047"}' ...
>> 
>> You missed the comma, by the way.
>> > 
>> > Tip: Avoid cryptic, meaningless tools like "sed" (and friends).
>> > 
>> > 
>> How is 'print "\047"$0"\047" less cryptic than the above sed line?
>> 
>> Sorry, I don't want to start an awk V sed flame war, but let's face it,
>> that so-called tip IS flame-bait.
>> 
>> Andrew
> 
> I think he was attempting a joke.

Most probably, after all he he really wanted to avoid cryptic stuff
he'd certainly would have given a simple plainest überclear shell
solution, for instance:
------------
$ cat blobber

exec 5<&0       ###     lets say stdin somewhere else
exec < "${1}"   ###     stdin becomes the file
while read l
do
        echo "'${l}',"
done
exec 0<&5 5<&-  ###     stdin back to normality
------------

or, keeping the first idea of "missing the comma" even
more 'readablest' toolbox solution:
------------
yes "'" | head -n $(wc -l yourfile | cut -f 1 -d" ")|tee - |paste -d "" - yourfile -
------------

but maybe "missing the comma" would be like 'jumping the shark' ;-)

(forgive me for what the heatwave's doing to my inner circuits ,-)
0
Reply Loki 7/24/2010 9:13:59 AM

Sat, 24 Jul 2010 09:13:59 +0000, Loki Harfagr did cat :
....
> exec 5<&0       ###     lets say stdin somewhere else exec < "${1}"  

just in case, it is probably better to avoid using that special number 5
can't remember the details but it was (at a time/forever/wot ?) used by
some bash in some situations for forks redirs, or something akin,
hum, gotta get back to read about this as I realize I've all
forgotten about these corner cases :-)
0
Reply Loki 7/24/2010 9:19:14 AM

In article <i2cf5j$982$1@south.jnrs.ja.net>,
Andrew McDermott  <a.p.mcdermott@rl.ac.uk> wrote:
>Kenny McCormack wrote:
>
>> In article <MP6dneh2BdV5CdTRnZ2dnUVZ_sydnZ2d@giganews.com>,
>> hehe2046  <user@compgroups.net/> wrote:
>>>I would say: sed -i "s/.*/'&',/" input.txt
>>>
>>>
>> 
>> Or, more sensibly:
>> 
>> awk '{print "\047"$0"\047"}' ...
>
>You missed the comma, by the way.
>> 
>> Tip: Avoid cryptic, meaningless tools like "sed" (and friends).
>> 
>
>How is 'print "\047"$0"\047" less cryptic than the above sed line?
>
>Sorry, I don't want to start an awk V sed flame war, but let's face it, that
>so-called tip IS flame-bait.
>
>Andrew

My point was that it is better to learn to use a tool that is likely to
help you more down the line.  I learned AWK pretty much as my first
tool, and never really bothered with sed/join/comm/tr/etc.  I think we
do people a disservice by giving them sed/join/comm/tr/etc solutions.

-- 
(This discussion group is about C, ...)

Wrong.  It is only OCCASIONALLY a discussion group
about C; mostly, like most "discussion" groups, it is
off-topic Rorsharch [sic] revelations of the childhood
traumas of the participants...

0
Reply gazelle 8/9/2010 1:24:51 PM

On 2010-08-09, Kenny McCormack wrote:
> In article <i2cf5j$982$1@south.jnrs.ja.net>,
> Andrew McDermott  <a.p.mcdermott@rl.ac.uk> wrote:
>>Kenny McCormack wrote:
>>
>>> In article <MP6dneh2BdV5CdTRnZ2dnUVZ_sydnZ2d@giganews.com>,
>>> hehe2046  <user@compgroups.net/> wrote:
>>>>I would say: sed -i "s/.*/'&',/" input.txt
>>>>
>>>>
>>> 
>>> Or, more sensibly:
>>> 
>>> awk '{print "\047"$0"\047"}' ...
>>
>>You missed the comma, by the way.
>>> 
>>> Tip: Avoid cryptic, meaningless tools like "sed" (and friends).
>>> 
>>
>>How is 'print "\047"$0"\047" less cryptic than the above sed line?
>>
>>Sorry, I don't want to start an awk V sed flame war, but let's face it, that
>>so-called tip IS flame-bait.
>>
>>Andrew
>
> My point was that it is better to learn to use a tool that is likely to
> help you more down the line.  I learned AWK pretty much as my first
> tool, and never really bothered with sed/join/comm/tr/etc.  I think we
> do people a disservice by giving them sed/join/comm/tr/etc solutions.

   It is better to learn the right tool for the job.

   To use awk to replace join/comm/tr/etc. requires coding when a
   simple command would suffice.

   For simple substitutions (and even some more complex ones), sed is
   just as comprehensible as awk.


-- 
   Chris F.A. Johnson, author           <http://shell.cfajohnson.com/>
   ===================================================================
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

0
Reply Chris 8/9/2010 7:30:45 PM

On Mon, 9 Aug 2010 19:30:45 +0000 (UTC), "Chris F.A. Johnson"
<cfajohnson@gmail.com> wrote:

>On 2010-08-09, Kenny McCormack wrote:

>> My point was that it is better to learn to use a tool that is likely to
>> help you more down the line.  I learned AWK pretty much as my first
>> tool, and never really bothered with sed/join/comm/tr/etc.  I think we
>> do people a disservice by giving them sed/join/comm/tr/etc solutions.
>
>   It is better to learn the right tool for the job.
>   To use awk to replace join/comm/tr/etc. requires coding when a
>   simple command would suffice.

I prefer minimal solutions, using the least capable tool that will solve
the problem.


-- 
Web mail, POP3, and SMTP
http://www.beewyz.com/freeaccounts.php
 
0
Reply John 8/9/2010 7:46:50 PM

In article <i3pl14$i1m$1@news.eternal-september.org>,
Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
....
>   To use awk to replace join/comm/tr/etc. requires coding when a
>   simple command would suffice.
>
>   For simple substitutions (and even some more complex ones), sed is
>   just as comprehensible as awk.

Obviously, I disagree.  I think it is better to learn a tool and use
that tool.  Why bother your head with stupid, virtually useless tools?

But I guess we can agree to disagree.

-- 
"The anti-regulation business ethos is based on the charmingly naive notion
that people will not do unspeakable things for money." - Dana Carpender

Quoted by Paul Ciszek (pciszek at panix dot com).  But what I want to know
is why is this diet/low-carb food author doing making pithy political/economic
statements?

But the above quote is dead-on, because, the thing is - business in one
breath tells us they don't need to be regulated (that they can morally
self-regulate), then in the next breath tells us that corporations are
amoral entities which have no obligations to anyone except their officers
and shareholders, then in the next breath they tell us they don't need to be
regulated (that they can morally self-regulate) ...

0
Reply gazelle 8/9/2010 8:07:34 PM

On Mon, 9 Aug 2010 20:07:34 +0000 (UTC), gazelle@shell.xmission.com (Kenny McCormack) wrote:

>In article <i3pl14$i1m$1@news.eternal-september.org>,
>Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>...
>>   To use awk to replace join/comm/tr/etc. requires coding when a
>>   simple command would suffice.
>>
>>   For simple substitutions (and even some more complex ones), sed is
>>   just as comprehensible as awk.
>
>Obviously, I disagree.  I think it is better to learn a tool and use
>that tool.  Why bother your head with stupid, virtually useless tools?

I learned awk, stopped using the cut, sed and friends simply because awk 
is easier for me to remember ;)  Sometimes efficiency is getting the job 
at hand done, accurately, not optimally unless there's other factors 
force use of a particular tool.
>
>But I guess we can agree to disagree.

Of course :)

Grant.
0
Reply Grant 8/9/2010 8:58:07 PM

In article <5lq066p5a4b9l2dp644r4dvvgobgh5aacg@4ax.com>,
Grant  <omg@grrr.id.au> wrote:
>On Mon, 9 Aug 2010 20:07:34 +0000 (UTC), gazelle@shell.xmission.com
>(Kenny McCormack) wrote:
>
>>In article <i3pl14$i1m$1@news.eternal-september.org>,
>>Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>>...
>>>   To use awk to replace join/comm/tr/etc. requires coding when a
>>>   simple command would suffice.
>>>
>>>   For simple substitutions (and even some more complex ones), sed is
>>>   just as comprehensible as awk.
>>
>>Obviously, I disagree.  I think it is better to learn a tool and use
>>that tool.  Why bother your head with stupid, virtually useless tools?
>
>I learned awk, stopped using the cut, sed and friends simply because awk 
>is easier for me to remember ;)  Sometimes efficiency is getting the job 
>at hand done, accurately, not optimally unless there's other factors 
>force use of a particular tool.

Well put, sir!

>>But I guess we can agree to disagree.
>
>Of course :)
>
>Grant.

We agree!

-- 
Faced with the choice between changing one's mind and proving that there is
no need to do so, almost everyone gets busy on the proof. 

    - John Kenneth Galbraith -

0
Reply gazelle 8/9/2010 9:07:47 PM

In article <pan.2010.07.24.09.19.14@thedarkdesign.free.fr.INVALID>,
Loki Harfagr  <l0k1@thedarkdesign.free.fr.INVALID> wrote:
>Sat, 24 Jul 2010 09:13:59 +0000, Loki Harfagr did cat :
>...
>> exec 5<&0       ###     lets say stdin somewhere else exec < "${1}"  
>
>just in case, it is probably better to avoid using that special number 5
>can't remember the details but it was (at a time/forever/wot ?) used by
>some bash in some situations for forks redirs, or something akin,
>hum, gotta get back to read about this as I realize I've all
>forgotten about these corner cases :-)

Nice time for a definition:  what's a "corner case"?

David

0
Reply dkcombs 8/23/2010 7:21:56 PM

On 2010-08-23, David Combs <dkcombs@panix.com> wrote:
> In article <pan.2010.07.24.09.19.14@thedarkdesign.free.fr.INVALID>,
> Loki Harfagr  <l0k1@thedarkdesign.free.fr.INVALID> wrote:
>>just in case, it is probably better to avoid using that special number 5
>>can't remember the details but it was (at a time/forever/wot ?) used by
>>some bash in some situations for forks redirs, or something akin,
>>hum, gotta get back to read about this as I realize I've all
>>forgotten about these corner cases :-)

> Nice time for a definition:  what's a "corner case"?

Generally, a special case where something unlikely happens that breaks an
assumption you made.  I'm not sure of the etymology, probably an extension
of "edge cases".

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
0
Reply Seebs 8/23/2010 7:25:10 PM

On 2010-08-23, David Combs wrote:
> In article <pan.2010.07.24.09.19.14@thedarkdesign.free.fr.INVALID>,
> Loki Harfagr  <l0k1@thedarkdesign.free.fr.INVALID> wrote:
>>Sat, 24 Jul 2010 09:13:59 +0000, Loki Harfagr did cat??:
>>...
>>> exec 5<&0       ###     lets say stdin somewhere else exec < "${1}"  
>>
>>just in case, it is probably better to avoid using that special number 5
>>can't remember the details but it was (at a time/forever/wot ?) used by
>>some bash in some situations for forks redirs, or something akin,
>>hum, gotta get back to read about this as I realize I've all
>>forgotten about these corner cases :-)
>
> Nice time for a definition:  what's a "corner case"?

    <http://en.wikipedia.org/wiki/Corner_case>

-- 
   Chris F.A. Johnson, author           <http://shell.cfajohnson.com/>
   ===================================================================
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

0
Reply Chris 8/23/2010 7:28:01 PM

Loki Harfagr wrote:

>> exec 5<&0       ###     lets say stdin somewhere else exec < "${1}"  
> 
> just in case, it is probably better to avoid using that special number 5
> can't remember the details [...]

The autoconf documentation (aiming at maximum portability by any
means, though), collects such information. But it doesn't mention fd 5.
http://www.gnu.org/software/autoconf/manual/html_node/File-Descriptors.html
Rather, it's using fd 5 in configure scripts itself.

Perhaps you had fd 4 in mind, which is an unconfirmed issue
on an extraterrestrial system called Kubota Titan.

In addition to the autoconf docs I know of problems with fd 19
(almost all Bourne shells) or fd 59 (HP-UX Bourne shell).
-- 
http://www.in-ulm.de/~mascheck/bourne/fd19.html
0
Reply Sven 8/23/2010 10:41:49 PM

In article <slrni75iom.il6.usenet-nospam@guild.seebs.net>,
 Seebs <usenet-nospam@seebs.net> wrote:

> On 2010-08-23, David Combs <dkcombs@panix.com> wrote:
> > In article <pan.2010.07.24.09.19.14@thedarkdesign.free.fr.INVALID>,
> > Loki Harfagr  <l0k1@thedarkdesign.free.fr.INVALID> wrote:
> >>just in case, it is probably better to avoid using that special number 5
> >>can't remember the details but it was (at a time/forever/wot ?) used by
> >>some bash in some situations for forks redirs, or something akin,
> >>hum, gotta get back to read about this as I realize I've all
> >>forgotten about these corner cases :-)
> 
> > Nice time for a definition:  what's a "corner case"?
> 
> Generally, a special case where something unlikely happens that breaks an
> assumption you made.  I'm not sure of the etymology, probably an extension
> of "edge cases".

Right.  The wikipedia page explains:

> The term "corner case" comes about by physical analogy with "edge case". 
> Where an edge case involves pushing one variable to a minimum or maximum, 
> putting us at the "edge" of the configuration space, a corner case involves 
> doing so with multiple variables, which would put us at a "corner" of a 
> multidimensional configuration space.

Although the example from Loki would probably be categorized more as an 
edge case than a corner case -- the distinction is probably lost on many 
people.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
1
Reply Barry 8/24/2010 2:38:42 AM

19 Replies
1912 Views

(page loaded in 0.207 seconds)

Similiar Articles:

















7/21/2012 2:37:08 AM


Reply: