LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-22-2015, 12:35 PM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 47

Rep: Reputation: Disabled
add tags to enclose a string


hi i have the following file which contains.

HOSTNAME1 randomstring1
HOSTNAME12 randomstring2

id like to change it to

<name>HOSTNAME1</name>
<string>randomstring1</string>

<name>HOSTNAME12</name>
<string>randomstring2</string>

Please help
 
Old 05-22-2015, 01:11 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,913
Blog Entries: 14

Rep: Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142
Create a script file with the following contents. (Substitute the name of your original file for "test.txt")

Code:
#!/bin/bash

cat test.txt | while read hostnm randstr
do echo \<name\>$hostnm\<\/name\>
   echo \<string\>$randstr\<\/string\>
   echo ""
done

The first line of above script tells it to use bash to do the rest of the script. It directs output of the command to the while loop which sets a variable for each item on each line it reads (in your case 2 items with first variable being called hostnm and second being called randstr). Note the variable names are arbitrary you could use billybob and ralph if you wanted.

The next line outputs the first item on each line calling the variable as $hostnm wraps the tagging you want.
The extra "" symbols you see are to escape the special meaning (< = redirect in normally, > = redirect standard output normally so you don't want to do that. The \< and \> says to use the symbols literally instead of using their special meanings.
The line after that does the same thing for the second item found on each input line.

This uses a "while loop" - it says to repeat for every line it finds. The "do" tells it what to do for every line and the "done" tells it to end the while loop.

Last edited by MensaWater; 05-26-2015 at 08:50 AM.
 
Old 05-22-2015, 03:37 PM   #3
bishop2001
Member
 
Registered: Apr 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
that's excellent. Thanks for the explanation as well.
 
Old 05-22-2015, 07:15 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,405

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
Or in one line
Code:
sed -n 's/^\(.*\) \(.*\)/<name>\1<\/name>\n<string>\2<\/string>/p' /tmp/xxx
This will print the result to stdout if you want to either redirect to another file or add th -i switch to sed to change in place.

-n means dont automatically print, 's/...' is search and replace, ^ means anchor to start of line, \(.*\) means match and number of characters and rememeber what was matched, \1 and \2 the first and second matches, p means print the substitution
 
Old 05-22-2015, 07:23 PM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,405

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
Code:
keithhedger@severnet ~ $ cat /tmp/xxx
HOSTNAME1 randomstring1
HOSTNAME2 randomstring2
keithhedger@severnet ~ $ sed -n 's/^\(.*\) \(.*\)/<name>\1<\/name>\n<string>\2<\/string>/p' /tmp/xxx
<name>HOSTNAME1</name>
<string>randomstring1</string>
<name>HOSTNAME2</name>
<string>randomstring2</string>
Sorry forgot to paste the results
 
Old 05-23-2015, 06:02 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
awk is not really any shorter in solution, but would be my choice for columned data:
Code:
awk '{print "<name>"$1"</name>"RS"<string>"$2"</string>"}' file
 
Old 05-23-2015, 07:30 AM   #7
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,405

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
that's the good thing about Linux there are always multiple solutions to most any problem.
 
  


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 add and delete a string in front of a string by sed? Ben Wang Linux - Newbie 6 05-16-2015 06:23 PM
Please add a sticky in the Programming forum telling people how to use code tags H_TeXMeX_H LQ Suggestions & Feedback 12 06-01-2013 03:19 AM
How to use Vi to match a string of text, add a new line, insert string... Slycraft Linux - Newbie 7 07-20-2012 10:24 AM
[SOLVED] Remove html tags with particular string inside georgi Linux - Newbie 4 05-03-2012 08:46 AM
how to add tags to avi file ? kunalagon Linux - Software 4 11-17-2006 01:57 PM

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

All times are GMT -5. The time now is 02:13 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