LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-21-2004, 01:58 PM   #1
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Rep: Reputation: 30
insert string with sed


I am writing a small bash script and I wanted to insert a string in front of every line of the file.
I tried to do something like that:

sed '/^/i\$STRING'

This doesn't work and I don't understand how to use an 'insert' option.

I need to insert my string from a variable and without a new line.

So let's say that:

STRING="beginning "

and line in the file is:

"and end"

then I want to make it:

"beginning and end"

I could also use different command. It doesn't have to be sed.
Thanks for any help.
 
Old 11-21-2004, 02:26 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 65
How about:
STRING="beginning"
sed s/^/$STRING/ somefile.txt
 
Old 11-21-2004, 02:47 PM   #3
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
This one works. Thanks.
But it only affects output to the terminal.
To change the file I have to do something like that:

cat somefile.txt | sed s/^/$STRING/ > somefile.txt

Is there a nicer way to actually change the file?
 
Old 11-21-2004, 02:54 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 65
You can use -i:
sed -i s/^/$STRING/ somefile.txt
 
Old 11-21-2004, 02:58 PM   #5
davidrr
LQ Newbie
 
Registered: Nov 2004
Posts: 3

Rep: Reputation: 0
If you do this:

cat somefile.txt | sed s/^/$STRING/ > somefile.txt

somefile.txt will be erased. Haven't you done the test ?
I suggest you the following:

sed s/^/$STRING/ somefile.txt > somefile.txt~ &&
mv -f somefile.txt~ somefile.txt
 
Old 11-21-2004, 03:09 PM   #6
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by davidrr
If you do this:

cat somefile.txt | sed s/^/$STRING/ > somefile.txt

somefile.txt will be erased. Haven't you done the test ?

Actually, it doesn't get erased. But thanks for the hint on alternative solution.


sed -i was what I needed.
Thanks
 
Old 11-22-2004, 02:11 AM   #7
abhijeetudas
Member
 
Registered: Sep 2004
Distribution: Redhat / Fedora
Posts: 114

Rep: Reputation: 15
sed -i

hi

i was following this post
i noticed that if i use

cat file.txt | sed s/^/$STRING/ > file.txt

Well it works fine but not more than 8192 bytes ..
Just thought id let u know..
hey by the way thanks for the -i option
din know about it..


- Thanks..
 
Old 02-18-2005, 02:11 PM   #8
buddy_epson
LQ Newbie
 
Registered: Jun 2003
Distribution: RH
Posts: 8

Rep: Reputation: 0
a thing of beauty

I'm a newbie to sed, but I've discovered how powerful it is, and simple to use once you get the syntax down. anway, I've been trying to find out (for awhile) how to do something as simple as inserting a character at the beginning of each line, and the manuals around are pretty much worthless. Just wanted to say thanks, because you've saved me a boatload of time and frustration on this one! (I wish I'd found it sooner)

Buddy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert CRLF's with sed? unSpawn Programming 6 11-22-2006 08:46 AM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 08:54 PM
insert a symbol with sed tonton Programming 5 08-31-2004 12:33 PM
sed command to insert tab? iluvatar Linux - Software 4 06-29-2004 02:56 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration