LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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: 169

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: 169

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: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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: 169

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: 169

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



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

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

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