Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-03-2010, 08:52 PM   #1
Registered: Oct 2005
Posts: 321

Rep: Reputation: 32
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

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

What I need to have is

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
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
while read -r line
  echo "<tag>$line</tag>"
done <"file" > newfile
mv newfile file
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
Registered: Oct 2005
Posts: 321

Original Poster
Rep: Reputation: 32

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



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 > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration