LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-04-2014, 09:04 AM   #1
tripialos
Member
 
Registered: Apr 2012
Posts: 166

Rep: Reputation: Disabled
append text with sed


Hi guys

I want to append a space to each line of a file that matches a specific pattern. For example:


Code:
this is test line
Another line
third line
Another test line
last line
I want to add a space to the lines that begin with the word "Another" so the text will transfrom to

Code:
this is test line
 Another line
third line
 Another test line
last line
Any ideas how you can do this with sed?
 
Old 11-04-2014, 09:25 AM   #2
bstaletic
Member
 
Registered: Apr 2014
Distribution: Arch
Posts: 39

Rep: Reputation: Disabled
Original file (let's call it test):
Code:
cat test
this is test line
Another line
third line
Another test line
last line
Command
Code:
cat test | sed s/Another/\ Another/
otputs:
Code:
this is test line
 Another line
third line
 Another test line
last line
So redircting the output of that command to a file (let's call it test.new) looks like:
Code:
cat test | sed s/Another/\ Another/ > test.new
And finally test.new looks like:
Code:
cat test.new

this is test line
 Another line
third line
 Another test line
last line
 
Old 11-04-2014, 09:38 AM   #3
tripialos
Member
 
Registered: Apr 2012
Posts: 166

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by bstaletic View Post
Original file (let's call it test):
Code:
cat test
this is test line
Another line
third line
Another test line
last line
Command
Code:
cat test | sed s/Another/\ Another/
otputs:
Code:
this is test line
 Another line
third line
 Another test line
last line
So redircting the output of that command to a file (let's call it test.new) looks like:
Code:
cat test | sed s/Another/\ Another/ > test.new
And finally test.new looks like:
Code:
cat test.new

this is test line
 Another line
third line
 Another test line
last line
YEap you are right. I was thinking about this method but i was afraid tha the whole line would be replaced...which apparently it didnt.

Well next time i should try first.

Thanks :-D
 
Old 11-04-2014, 09:47 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,076

Rep: Reputation: 861Reputation: 861Reputation: 861Reputation: 861Reputation: 861Reputation: 861Reputation: 861
Quote:
Originally Posted by tripialos View Post
...
Well next time i should try first.

Thanks :-D
thats the exact reason i didnt reply to your post. it seemed like a homework question and you showed no effort.
 
Old 11-04-2014, 10:19 AM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,184

Rep: Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584
Quote:
Originally Posted by tripialos View Post
Well next time i should try first.
In reality, if you don't learn to do that soon, your degree/diploma will be pointless.

Also, there are about a million examples of this available from a simple internet search, yet you had to sign up at a forum and ask the question directly. That isn't showing much ambition on your part.

Learn to research issues, and use the documentation and manuals available to you. Learning how to learn, and educating yourself constantly, is the most important step in your education.
 
Old 11-12-2014, 04:24 AM   #6
tripialos
Member
 
Registered: Apr 2012
Posts: 166

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
thats the exact reason i didnt reply to your post. it seemed like a homework question and you showed no effort.
Hey

This is not a homework. I actually tried alot of methods and ways and placed alot of efford but none of them worked.

After 10 hours on the keyboard:
Some times the answear is right in front of you but you cant see it.
Some times the answear is more simple than you think it is.
 
Old 11-12-2014, 04:33 AM   #7
tripialos
Member
 
Registered: Apr 2012
Posts: 166

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
In reality, if you don't learn to do that soon, your degree/diploma will be pointless.

Also, there are about a million examples of this available from a simple internet search, yet you had to sign up at a forum and ask the question directly. That isn't showing much ambition on your part.

Learn to research issues, and use the documentation and manuals available to you. Learning how to learn, and educating yourself constantly, is the most important step in your education.
Hi m8.

Thanks for you advises.

I finised my degree and my masters long time ago. Best thing i earned was not the titles but "how to learn".

I have subscribed long time ago on this forum also as a donador hence this is not actually the reason i subscribed here.

Indeed there were loads of examples on the net but when you "false" understood a certain basic fact in your head then all solutions will look false in your eyes.

Personally "i am discovering the world" almost everyday (meaning i learn something obvius which puzzle up things i had in my mind) which i am happy for.

As previusly stated, my mistake was that i took for granted those solutions wont work.

Again thanks for your advises :-)

all the best
 
  


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 append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
how can i append the text after pattern match using variable in sed ravikushal Linux - Newbie 18 07-23-2013 08:56 AM
[SOLVED] Pulling text from a text file and append to CSV fishy Linux - Newbie 5 03-02-2012 07:32 AM
[SOLVED] Append text with AWK or SED? LostChild1 Linux - Newbie 6 03-20-2010 08:46 AM
sed script to append variable text gmartin Linux - General 4 12-27-2006 04:44 PM


All times are GMT -5. The time now is 05:31 PM.

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