LinuxQuestions.org
Visit Jeremy's Blog.
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 08-03-2010, 08:52 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 280

Rep: Reputation: 30
how to add string on the beginning and end of the line


I need to output of a script to a file with tags.

I'm trying sed and was able to put a tag on the end of the line

Code:
cat lists.txt | sed 's/$/\<\/group\>/'
Output is

Quote:
foo</group>
fee</group>
What I need to have is

Quote:
<group>foo</group>
<group>fee</group>
I'm thinking of I redirect my first sed command to a file and then use sed again to put <group> at the beginning of the line. My problem is how to put <group> to all beginning of the string

Last edited by packets; 08-03-2010 at 08:57 PM.
 
Old 08-03-2010, 09:10 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
lose the cat. Its useless.
for the front of the line, use 's/^/<tag>/'

another way to visualize putting things in front and behind a string, is just to print them out as you iterate the file
Code:
while read -r line
do
  echo "<tag>$line</tag>"
done <"file" > newfile
mv newfile file
Code:
awk '{print "<tag>"$0"</tag>"}' file
that's it. No need for any regex.

Last edited by ghostdog74; 08-03-2010 at 09:17 PM.
 
Old 08-03-2010, 09:55 PM   #3
packets
Member
 
Registered: Oct 2005
Posts: 280

Original Poster
Rep: Reputation: 30
@ghostdog74

I'm playing with awk before almost the same command as yours. What a noob am I!

Thanks!
 
  


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
[SOLVED] How to Read a String and add it at the end in Unix funonnet Programming 7 06-11-2010 09:53 AM
How could i add to the beginning of line word? DoME69 Programming 6 09-17-2008 02:55 AM
Append variable string(s) at end of each line schaganti Linux - Newbie 2 10-19-2007 01:31 PM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM
Wierd String appended to end of line lel800 General 8 12-28-2004 03:55 PM


All times are GMT -5. The time now is 08:10 AM.

Main Menu
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