Insert tags in lines after searching for a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert tags in lines after searching for a word
# 1  
Old 09-17-2010
Insert tags in lines after searching for a word

Hi, I am new to Unix and this is my first post on this forum. I am trying to convert a file into an xml. In my input I want to search for any line that starts with a 'F' and make it a tag in the xml.

See below for the input and output.

Input :
Code:
<Payment>
<REFERENCE>78</REFERENCE>
F123  :  12345678
F12A  :  23424234234
F59   :  345345345345
</Payment>
<Payment>
<REFERENCE>79</REFERENCE>
F10F  :  456456456456
F22A  :  345345345345
</Payment>

Desired Output :
Code:
<Payment>
<REFERENCE>78</REFERENCE>
<F123>12345678</F123>
<F12A>23424234234</F12A>
<F59>345345345345</F59>
</Payment>
<Payment>
<REFERENCE>79</REFERENCE>
<F10F>456456456456</F10F>
<F22A>345345345345</F22A>
</Payment>

I tried looping through the file and got the desired output but since my file is huge it take a lot of time. Is there a way to do this using sed or awk?

Please advice.


Moderator's Comments:
Mod Comment Please use code tags, thank you

Last edited by Franklin52; 09-17-2010 at 10:41 AM..
# 2  
Old 09-17-2010
Can you show us what have you tried so far and where you are stuck?
# 3  
Old 09-17-2010
this is what I have done and it works fine except for the fact that this is very slow on a file with more than 30000 lines.

Code:
#!/bin/sh
outputfile=1.xml
inputfile=1.bak
iCurrentLine=1
LineCount=`wc -l $inputfile|awk '{print $1}'`
while [ "$iCurrentLine" -le "$LineCount" ]
do
       CurrentLine=`head -$iCurrentLine $inputfile|tail -1`
       j=`echo $CurrentLine|grep "^F[0-9][0-9]"`
       if [ "$j" != "" ]; then
               tag=`echo $CurrentLine | cut -c1-4`
               text=`echo $CurrentLine | cut -c8-0`
               Line="<$tag>"$text"</$tag>"
       else
       	       echo $CurrentLine >> $outputfile
       fi
       iCurrentLine=`expr $iCurrentLine + 1`
done


Last edited by Scott; 09-17-2010 at 12:40 PM.. Reason: Please use code tags
# 4  
Old 09-17-2010
If the input is really like you said, this should work i guess :

Code:
awk '/^</{print $0}/^[A-Z]/{print "<"$1">"$3"<"$1">"}' inputfile >outputfile

This User Gave Thanks to Chirel For This Post:
# 5  
Old 09-17-2010
Code:
# sed '/^F/s/^\(.[^ ]*\) *: *\(.*\)/<\1>\2<\1>/' infile
<Payment>
<REFERENCE>78</REFERENCE>
<F123>12345678<F123>
<F12A>23424234234<F12A>
<F59>345345345345<F59>
</Payment>
<Payment>
<REFERENCE>79</REFERENCE>
<F10F>456456456456<F10F>
<F22A>345345345345<F22A>
</Payment>

This User Gave Thanks to ygemici For This Post:
# 6  
Old 09-17-2010
both the sed nad awk worked like a charm! thanks for the help.
# 7  
Old 09-17-2010
awk or sed would be the optimal solution, like the solutions earlier (though they both need a / in the closing tags). Another version would be:
Code:
awk '!/^</{$0="<"$1">"$3"</"$1">"}1' infile>outfile

Although a script will be slower, your script above can be optimized, so that speed might still be acceptable, for instance:
Code:
#!/bin/sh
outputfile=1.xml
inputfile=1.bak
while read line; do
  case $line in
    "<"*) echo "$line" ;;
    *)    set -- $line
          echo "<$1>$3</$3>" ;;
  esac
done < "$inputfile" > "$outputfile"

ksh93 or dash are usually the fastest shells in my experience

Last edited by Scrutinizer; 09-17-2010 at 02:33 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert Word After Match

Dear ALL, I have sample file : host.txt fullname: FreeBSD Host: server1 ip: 1.1.1.1 mask: 255.255.255.0 fullname: CentOS Host: server2 mask: 255.255.255.0 fullname: Fedora Host: server3 ip: 1.3.1.2 mask: 255.255.255.0 (2 Replies)
Discussion started by: gnulyn
2 Replies

2. Shell Programming and Scripting

Insert Text after searching via grep

Hi Team, I have a file with the following patterns: ==> xyz_Server_Started_with_Errors <== errors. ==> abc_Server_Started_with_Errors <== errors ==> reds_Server_Started_with_Errors <== errorss I want them in this format: (5 Replies)
Discussion started by: ankur328
5 Replies

3. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

4. UNIX for Dummies Questions & Answers

Insert Text on lines having the string word

I need help on how I can accomplish my task. I hope someone can help me since I've researching and trying to accomplish this for hours now. Basically, I need to comment-out (or insert a # sign in the beginning of the line) a line when the line has the specific word I am searching. Example I have... (3 Replies)
Discussion started by: Orbix
3 Replies

5. Shell Programming and Scripting

Searching a word in multiple files

Hi All, I have a issue in pulling some heavy records , I have my input file has 10,000 records which i need to compare with daily appended log files from (sep 1st 2009 to till date) . I tried to use grep fgrep and even sed , but the as time is factor for me , i cannot wait for 5 days to get the... (3 Replies)
Discussion started by: rakesh_411
3 Replies

6. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

7. Shell Programming and Scripting

searching in a while where a word is not there.

Hi, I am new to unix, pls help I have a file suc_Logfile file. liek this. output success success abc Now i need to generate a file in shell script where it shows only abc. Now i am doing like this grep "^successfully\|$" $suc_Logfile >> $Final_Suc Pls help. Thanks,... (6 Replies)
Discussion started by: sailaja_80
6 Replies

8. Shell Programming and Scripting

Searching word in a file with awk

Hello everyone! I use a script to query for installed packages with yum (I use RHEL 4 with yum installed) and the output is redirected to a file. My script scan this file to find a package and the version of it. The script works fine until I search for a package name with special characters.... (3 Replies)
Discussion started by: studieu
3 Replies

9. UNIX for Dummies Questions & Answers

searching word in files

hi all i want to search some word in file but i want that it will search in all the files that i have in all the directories i do'nt know the name of the file i know that grep command want some specified file in which command shell i used thanks (2 Replies)
Discussion started by: naamas03
2 Replies

10. UNIX for Dummies Questions & Answers

Searching for key word within a file

Hello, I have a directory that holds all of my matlab codes. I am trying to run a searh on all of the matlab files that have the word "while" written inside the sytax of the code. Looking for all of the times that I did a while loop. Can someone help me do this? Thanks in advance. ... (1 Reply)
Discussion started by: moradwan
1 Replies
Login or Register to Ask a Question