Need to extract characters between two search words in a script!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to extract characters between two search words in a script!!
# 1  
Old 04-17-2015
Wrench Need to extract characters between two search words in a script!!

Hi,

I have a log file which is the output from a xml script :

Code:
<?xml version="1.0" ?>
<!DOCTYPE svc_result SYSTEM "MLP_SVC_RESULT_320.DTD">
  <svc_result ver="3.2.0">
    <slia ver="3.0.0">
      <pos>
        <msid type="MSISDN" enc="ASC">8093078040</msid>
        <poserr>
          <result resid="5">ABSENT SUBSCRIBER</result>
          <add_info>Country Code: 1  Country Id: DOM</add_info>
          <time utc_off="-0400">20150416233219</time>
        </poserr>
        <gsm_net_param>
          <neid>
            <vmscid>
              <cc>1</cc>
              <ndc>829</ndc>
              <vmscno>3069998</vmscno>
            </vmscid>
          </neid>
        </gsm_net_param>
      </pos>
    </slia>
  </svc_result>
*

Now my task is to print the character present before </msid> and before </result> to another file so that output is something like this:

Code:
8093078040    ABSENT SUBSCRIBER
8738383838    ABSENT SUBSCRIBER

Any idea pls..!!


Moderator's Comments:
Mod Comment Use code tags, thanks. There are online XML-formatters, really handy...

Last edited by zaxxon; 04-17-2015 at 10:58 AM..
# 2  
Old 04-17-2015
You can check out this as a start:
parsing xml using awk
# 3  
Old 04-17-2015
Hi,
In bash with grep (with option support -o and -P) and paste:
Code:
paste - - < <(grep -oP '[^>]*(?=</(msid|result))' file)

Regards.
# 4  
Old 04-17-2015
For those that need something more portable or just hate awk:

Code:
sed -n -e 's;.*<msid[ ]*[^>]*>\([^<]*\).*;\1;p' \
       -e 's;.*<result[ ]*[^>]*>\([^<]*\).*;^I\1^B;p'  |
tr -d '\012' |
tr '\002' '\012'

The ^I and ^B are control characters (tab and Ctrl-B)
# 5  
Old 04-17-2015
Thanks guys
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

2. UNIX for Beginners Questions & Answers

Non-root script used search and list specific key words

Hy there all. Im new here. Olso new to terminal & bash, but it seams that for me it's much easyer to undarsatnd scripts than an actual programming language as c or anyother languare for that matter. S-o here is one og my home works s-o to speak. Write a shell script which: -only works as a... (1 Reply)
Discussion started by: Crisso2Face
1 Replies

3. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

Script to search and extract the gene sub-location from gff file.

Hi, my problem is that I have two files. File no. 1 is a gff text file (say gi1) that has gene information like : ******************** gene 39389788..39395643 /gene="RPSA" /note="Derived by automated computational analysis using ... (2 Replies)
Discussion started by: reena2305
2 Replies

5. Shell Programming and Scripting

Get characters between two words

Guys, Here is the txt file... SLIC N0SLU704034789 rŒ° EJ00 ó<NL DMRG>11 100 4B 2 SLIC N0SLU704034789 rŒ° TJ10 <4000><NL> 2 SLIC N0SLU704034789 ... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

6. Shell Programming and Scripting

help with Scripting - trying to search for string and extract next few characters

Hi I am new to world on unix scripting so any assistance would be gratefully appreciated, I am trying to write a script which reads through a file, reads in line by line, searches for a pattern, copies string after it and then to do a search and replace elsehwere in the line, so the... (7 Replies)
Discussion started by: LonJ_80
7 Replies

7. Shell Programming and Scripting

extract characters from file name - script

trying to extract the numbers in this file name: fname="ebcdic.f0633.cmp_ebcdic.f0633.bin" fnametmp=${fname#*(V|v|F|f)} parse=${fnametmp%%(ENC|enc|CMP|cmp|BIN|bin)}} echo FLRECL=$parse result is FLRECL=0633.cmp_ebcdic.f0633 expected result FLRECL=0633 my guru is on holiday and i need... (5 Replies)
Discussion started by: mambo2523
5 Replies

8. Shell Programming and Scripting

Perl script to search and extract using wildcards.

Good evening All, I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. ... (3 Replies)
Discussion started by: CammyD
3 Replies

9. Shell Programming and Scripting

Script for pulling words of 4 to 7 characters from a file

Even just advice on where to start would be helpful. Thank You (2 Replies)
Discussion started by: Azeus
2 Replies

10. HP-UX

extract field of characters after a specific pattern - using UNIX shell script

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could... (2 Replies)
Discussion started by: jansat
2 Replies
Login or Register to Ask a Question