Add tags in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add tags in a file
# 1  
Old 07-10-2009
Add tags in a file

I have a file with some IPs, and i want to add some tags to them using awk. The file will contain any number of IPs, and what i want is to add tags to all of them. For example:

original.txt
172.17.110.24
132.15.12.34
.....

what i want to get is

<device> 172.17.110.24 </device>
<device> 132.15.12.34 </device>
<device>.... </device>

I know that is a stupid question, but i couldn't find any similar examples.
Thanks in advance
# 2  
Old 07-10-2009
Code:
awk '{print "<device> " $0 " </device>"}' original.txt

# 3  
Old 07-10-2009
Given your sample data a simple printf should work:

Code:
printf '<device>%s</device>\n' $(<infile)

If you want to modify the existing file in-place:

Code:
perl -i.bck -pe's|[\d.]+|<device>$&</device>|' infile

# 4  
Old 07-10-2009
thanks to both of u! but in the first solutions it doesnt modify the file thought Smilie it just prints the solution in my shell. how is that possible?
# 5  
Old 07-10-2009
You need to redirect the output or use tee.
Code:
awk '{print "<device> " $0 " </device>"}' original.txt | tee original.txt
or
awk '{print "<device> " $0 " </device>"}' original.txt > new.txt
mv new.txt original.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I add a log file path to a vi file to monitor all the changes made to the file?

I'm curious to know how do I add an empty log file (test1.log) to an existing text file to monitor all the changes made to a.txt. Is this expression export PATH=$PATH:/home/test1.log right to be added to the text file a.txt? (5 Replies)
Discussion started by: TestKing
5 Replies

2. What is on Your Mind?

JQuery to Add Code Tags to Selected Text

Hey. Someone find or write some jQuery code where we can select text with our mouse and then click or double click the highlighted / selected text and then it will wrap code tags around the highlighted text (in our editors). :) (0 Replies)
Discussion started by: Neo
0 Replies

3. Shell Programming and Scripting

Add HTML tags to file list

Hi there, I am new to shell scripting and have been struggling with this example. I have an input variable that looks like that: FILELIST="100_*_123.txt" that will produce a list of files if you use ls ${FILELIST} The output looks like 100_EN_123.txt 100_FR_123.txt I am building... (4 Replies)
Discussion started by: ornesey
4 Replies

4. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies
Login or Register to Ask a Question