LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-13-2007, 10:52 AM   #1
mike_stlouis
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Rep: Reputation: 0
stupdi 'sed' question


i must have either a typo in this command, or not quite get the sed command completely.
i have a file with 20000 lines and i need to add a string 'test' to the begining of every line.

cat myfile.txt | sed s/'/^/'/test//' > myfile2.txt

this creates file - myfile2.txt but it's empty. i know this has gotta be simple but an elegant solution eludes me right now. thanx in advance.
 
Old 11-13-2007, 11:02 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

That should be: sed -i 's/^/test/' myfile.txt no need for the extra /'s and one of the '.

The -i makes it possible not to use the cat and redirection (-i = inplace, see manpage for other details).

Hope this clears things up a bit.
 
Old 11-13-2007, 01:57 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
My favorite SED tutorial: http://www.grymoire.com/Unix/Sed.html

Note the general form of the "s" (substitute) command:

sed s_old_new_X filename

where:
_ = the delimiter (whatever character follows the "s" is automatically the delimiter
old = the text to replace (in your example "^" means "the empty string at the beginning of the line")
new = the new text
X = optional commands--eg: "g" for replace all instances, "p" to print, "2" to only replace the 2nd occurrence.

The single quotes are not always required but are good practice. They say to the shell: "everything in here is for SED and not for you."

Doesn't your version produce an error message? If not, does anyone know why?
 
  


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
SED and MV question wendtbd Red Hat 4 08-23-2006 11:22 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed question ckoniecny Linux - General 3 11-11-2005 09:31 AM
sed question zoomzoom Linux - Software 2 10-20-2003 04:04 PM
A question on 'sed' Barbarian Programming 4 04-11-2002 10:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:47 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
Open Source Consulting | Domain Registration