Extracting particular string in a file and storing matched string in output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting particular string in a file and storing matched string in output file
# 1  
Old 03-08-2010
Extracting particular string in a file and storing matched string in output file

Hi ,
I have input file and i want to extract below strings

<msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets
<resCode>3000</resCode> => 3000 needs to be extracted
<resMessage>Request time
getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word Request timed out needs to be extarcted

Input file

Code:
getBalances_PSM.c(37):     D><msisdn xmlns="">0492001956</msisdn><counterListFlag xmlns="">0</counterListFlag></getBa
getBalances_PSM.c(37):     lances></soap:Body></soap:Envelope>

getBalances_PSM.c(37): t=169119ms: 342-byte response body for "https://10.144.0.53/apc/webservices/customeraccount" (RelFrameId=1, Internal ID=2)
getBalances_PSM.c(37):     <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getB
getBalances_PSM.c(37):     alancesResponse xmlns:ns2="http://www.al.com/apc/generated/services/customeraccount"><bund
getBalances_PSM.c(37):     leList/><accountLocation></accountLocation><resCode>3000</resCode><resMessage>Request time
getBalances_PSM.c(37):     d out</resMessage></ns2:getBalancesResponse></soap:Body></soap:Envelope>

Outputfile: It should be like

Code:
0492001956|3000|Requests Timed out

# 2  
Old 03-08-2010
You can use the following code

The file content is <msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets
<resCode>3000</resCode> => 3000 needs to be extracted
<resMessage>Request timed out</resMessage>
</ns2:getBalancesResponse> => the word Request timed out needs to be extarcted


Code:
 
use strict;
use warnings;
my($var);
open(FH,"<file1") or die "Can't open";
while($var=<FH>)
{
    if($var=~/<msisdn xmlns="">([0-9]{1,})<\/ msisdn>/)
    {
        print "$1|";
    }
    if($var=~/<resCode>([0-9]{1,})<\/resCode>/)
    {
        print "$1|";
    }
    if($var=~/<resMessage>([a-zA-Z ].*)<\/resMessage>/)
    {
        print "$1\n";
    }
}

The output is
0492001956|3000|Request timed out
# 3  
Old 03-09-2010
Hi,
Thanks for your reply. But I am getting syntax error for first 3 lines in bash shell. I think If we do search line by line it is taking too much of time since input file is big. Is there any way to do using awk or sed command?
# 4  
Old 03-09-2010
Till some one comes up with a better one...
Inputfile:
Code:
getBalances_PSM.c(37):     D><msisdn xmlns="">0492001956</msisdn><counterListFlag xmlns="">0</counterListFlag></getBa
getBalances_PSM.c(37):     lances></soap:Body></soap:Envelope>

getBalances_PSM.c(37): t=169119ms: 342-byte response body for "https://10.144.0.53/apc/webservices/customeraccount" (RelFrameId=1, Internal ID=2)
getBalances_PSM.c(37):     <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getB
getBalances_PSM.c(37):     alancesResponse xmlns:ns2="http://www.al.com/apc/generated/services/customeraccount"><bund
getBalances_PSM.c(37):     leList/><accountLocation></accountLocation><resCode>3000</resCode><resMessage>Request time
getBalances_PSM.c(37):     d out</resMessage></ns2:getBalancesResponse></soap:Body></soap:Envelope>

Code:
Code:
awk -F: 'NF{for(i=1; ++i<=NF;) printf $i}' infile | awk -F">|<" '{print $4"|"$30"|"$34}'

Output:
Code:
0492001956|3000|Request time     d out

I hope you can handle those spaces...

Last edited by malcomex999; 03-10-2010 at 02:01 AM.. Reason: changing infile location
# 5  
Old 03-09-2010
Yet another Perl solution:

Code:
$
$
$ cat -n data.txt
     1  getBalances_PSM.c(37):     D><msisdn xmlns="">0492001956</msisdn><counterListFlag xmlns="">0</counterListFlag></getBa
     2  getBalances_PSM.c(37):     lances></soap:Body></soap:Envelope>
     3
     4  getBalances_PSM.c(37): t=169119ms: 342-byte response body for "https://10.144.0.53/apc/webservices/customeraccount" (RelFrameId=1, Internal ID=2)
     5  getBalances_PSM.c(37):     <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/enve...Body><ns2:getB
     6  getBalances_PSM.c(37):     alancesResponse xmlns:ns2="http://www.al.com/apc/generated/serv...account"><bund
     7  getBalances_PSM.c(37):     leList/><accountLocation></accountLocation><resCode>3000</resCode><resMessage>Request time
     8  getBalances_PSM.c(37):     d out</resMessage></ns2:getBalancesResponse></soap:Body></soap:Envelope>
$
$ perl -lne 'BEGIN{undef $/} s/^.*:\s+|\n//mg; s/^.*<msisdn xmlns="">(\d+)<.*resCode>(\d+)<.*resMessage>([^<]+)<.*$/$1\|$2\|$3/; print' data.txt
0492001956|3000|Request timed out
$
$

tyler_durden
# 6  
Old 03-10-2010
Hi Taylor,
Thanks for your reply. I want same code in shell script not in perl. If you know please send the same.
# 7  
Old 03-10-2010
Code:
awk -F ":" '{printf $2}' filename | sed 's/.*<msisdn[^0-9]*\([0-9][0-9]*\)<\/msisdn.*<resCode>\([0-9]*\)<\/resCode.*<resMessage>\(.*\)<\/resMessage.*/\1|\2|\3/g'


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding specific string in file and storing in another file

Text in input file is like this <title> <band height="21" isSplitAllowed="true" > <staticText> <reportElement x="1" y="1" width="313" height="20" key="staticText-1"/> <box></box> <textElement> <font fontName="Arial" pdfFontName="Helvetica-Bold"... (4 Replies)
Discussion started by: aankita30
4 Replies

2. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

3. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

4. Shell Programming and Scripting

Search for Pattern as output between the matched String

Hello, I have a file which has the below contents : VG_name LV_name LV_size in MB LV_option LV_mountpoint owner group y testdg rahul2lv 10 "-A y -L" /home/abc2 ... (6 Replies)
Discussion started by: rahul2662
6 Replies

5. UNIX for Dummies Questions & Answers

Commenting a line matched with a specific string in a file

Hi, I would like to comment a line that matched a string "sreenivas" in a file without opening it. Thanks in advance. Regards, Sreenivas (3 Replies)
Discussion started by: raosr020
3 Replies

6. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

7. Shell Programming and Scripting

save every line in log file with matched string

i have been doing this script to match every line in a current log file (access_log) with strings that i list from a path (consist of 100 of user's name ex: meggae ).. and then make a directory of every string from the text file (/path/meggae/) --->if it matched.. then print every line from the... (3 Replies)
Discussion started by: meggae
3 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question