LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-10-2011, 01:19 AM   #1
ssgupta
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Rep: Reputation: 0
Post Command for appending a text at the start of every line and save it another file


Hi All,

I wish to append a text at the start of every line and save it to another file without changing the original file. Please let me know the command

For example:
Orignal.txt
a.txt
b.txt
c.txt

Newfile.txt
../top/a.txt
../top/b.txt
../top/c.txt

thanks
 
Old 03-10-2011, 01:41 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892
Check out sed with the -i option and the carat symbol (^) for your regex
 
Old 03-10-2011, 03:00 AM   #3
ssgupta
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Dear Grail,

Thanks for your input.

But can you elaborate the syntax. Could you please give an example?
 
Old 03-10-2011, 03:13 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
sed 's#^.*#../top/&#' infile >outfile
or
sed -i 's#^.*#../top/&#' infile.

The second uses a temp file in the background, to edit the original. the -i stands for inline.

There is an older edition of Sed and Awk available on the web from O'Reilly. I'd recommend downloading it or buying the latest edition. It is very good.
 
Old 03-10-2011, 03:23 AM   #5
ssgupta
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Thanks jschiwal and grail..
it works wonderfully.
 
Old 03-10-2011, 08:35 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892
Well I was trying to lead you to the answer to see what you would try, but glad you have solved your problem. Please mark the question as such
 
  


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 text to a file using command line replica88 Linux - Newbie 4 01-28-2010 05:50 PM
Adding line of text to start of file roadrash Linux - General 2 10-26-2009 07:40 PM
need to write a shell script for appending text into each line of a file nandakv4 Programming 9 08-15-2009 03:32 AM
Appending to a certain line in text w3stfa11 Programming 5 11-09-2006 09:33 AM
chpasswd from a command line (without a text file) mpgram Linux - Security 1 04-18-2004 05:44 PM

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

All times are GMT -5. The time now is 10:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration