Fetching the string using different patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetching the string using different patterns
# 1  
Old 04-26-2012
Data Fetching the string using different patterns

HI,

Please help me with the following problem:
I have an xml file with the following lines

Code:
<NameValuePair>
            <name>SharedResources/JDBC/Admin/password</name>
            <value>rjmadmin</value>
        </NameValuePair>
        <NameValuePair>
            <name>SharedResources/JDBC/Admin/userName</name>
            <value>rjmadmin</value>
        </NameValuePair>
        <NameValuePair>
            <name>SharedResources/JDBC/Batch/Password</name>
            <value>rjmbatch</value>
        </NameValuePair>
        <NameValuePair>
            <name>SharedResources/JDBC/Batch/UserName</name>
            <value>rjmbatch</value>
        </NameValuePair>
        <NameValuePair>
            <name>SharedResources/JDBC/Report/password</name>
            <value>rjmreport</value>
        </NameValuePair>
        <NameValuePair>
            <name>SharedResources/JDBC/Report/username</name>
            <value>rjmreport</value>
        </NameValuePair>
        <NameValuePair>
            <name>SharedResources/JDBC/SID</name>
            <value>RJMWRX01</value>
        </NameValuePair>
        <NameValuePair>
            <name>SharedResources/JDBC/View/password</name>
            <value>rjmview</value>
        </NameValuePair>
        <NameValuePair>
            <name>SharedResources/JDBC/View/username</name>
            <value>rjmview</value>

and i am having another .txt file containing

Code:
SharedResources/JDBC/Admin/username
SharedResources/JDBC/Admin/password
SharedResources/JDBC/Batch/UserName
SharedResources/JDBC/Batch/Password
SharedResources/JDBC/Report/username
SharedResources/JDBC/Report/password
SharedResources/JDBC/View/username
SharedResources/JDBC/View/password

now by using this txt file i have to fetch the corresponding values and store it into another file as

Code:
rjmadmin
rjmadmin
rjmbatch
rjmbatch
rjmreport
rjmreport
rjmview
rjmview

The code which i written is as below,

Code:
while read line
do
fgrep -e $line -A 1 Sample.xml >> output.txt
done<"values.txt"
sed 's!.*<value>\(.*\)</value>.*!\1!' output.txt > final.txt

the ouput is

Code:
Sample.xml:            <name>SharedResources/JDBC/Admin/password</name>
rjmadmin
Sample.xml:            <name>SharedResources/JDBC/Batch/UserName</name>
rjmbatch
Sample.xml:            <name>SharedResources/JDBC/Batch/Password</name>
rjmbatch
Sample.xml:            <name>SharedResources/JDBC/Report/username</name>
rjmreport
Sample.xml:            <name>SharedResources/JDBC/Report/password</name>
rjmreport
Sample.xml:            <name>SharedResources/JDBC/View/username</name>
rjmview
Sample.xml:            <name>SharedResources/JDBC/View/password</name>
rjmview


Please Help...Smilie

Moderator's Comments:
Mod Comment Please click this link: How to use [code] tags

Last edited by Scrutinizer; 04-26-2012 at 12:15 PM..
# 2  
Old 04-26-2012
Hi, try:
Code:
sed 'N;s!.*<value>\(.*\)</value>.*!\1!'

You can use a pipe instead of an intermediate file:

Code:
while read line
do 
  grep -Fe "$line" -A 1 Sample.xml
done < values.txt | sed 'N;s!.*<value>\(.*\)</value>.*!\1!' > final.txt

or combine things in awk
Code:
awk 'NR==FNR{A[$1];next} $3 in A && getline{print $3}' values.txt FS='[<>]' Sample.xml > final.txt


Last edited by Scrutinizer; 04-26-2012 at 12:36 PM..
# 3  
Old 04-26-2012
Hi,
I tried with threee codes
For the first two codes, I am getting same output which i am getting earlier.
there is no output for third code.

Please help me..
# 4  
Old 04-26-2012
Yet another awk 1 liner...
Code:
awk -F"<|>" '{if($3 in x){getline;print $3}else x[$1]}' txtfile xmlfile > outfile

# 5  
Old 04-26-2012
Quote:
Originally Posted by tejastrikez
Hi,
I tried with threee codes
For the first two codes, I am getting same output which i am getting earlier.
there is no output for third code.

Please help me..
Strange I get this with all three:
Code:
rjmadmin
rjmbatch
rjmbatch
rjmreport
rjmreport
rjmview
rjmview

What is your OS and version?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get string between 2 patterns

Hi all, I wish to get the string between u' and ' This is the test.txt file: PLAY *********************************** GATHERING FACTS ***************************** OK: TASK: **************************** changed: TASK: *************************** ok: => {"msg":... (11 Replies)
Discussion started by: celine
11 Replies

2. Shell Programming and Scripting

String search between patterns using sed

Hi, I am trying to find a way to get sed/awk/grep to help me find a string in a log file that exists between two datestamps and then print the preceding datestamp up to the next datestamp. Here is an example of my logfile: +++ 2013/03/28 17:01:37.085 SIGNALING HIGH ACTIVE Failure Response... (5 Replies)
Discussion started by: raytx
5 Replies

3. Shell Programming and Scripting

Fetching string after matching pattern from last

I have a file a file having entries are like @ram@sham@sita @krishan@kumar @deep@kumar@hello@sham in this file all line are having different no of pattern-@. need to fetch the substring after the last pattern. like sita kumar sham thanks in advance (3 Replies)
Discussion started by: saluja.deepak
3 Replies

4. Shell Programming and Scripting

Getting a string from between two patterns

I need something to strip out some text from a string. I basically have a variable that will always be in the same pattern but will be a different length. Example below. I need to somehow extract the text between <Strat> and </Source> from example below. I can't load it into a file for audit... (5 Replies)
Discussion started by: atelford
5 Replies

5. UNIX for Dummies Questions & Answers

replace multiple patterns in a string/filename

This should be somewhat simple, but I need some help with this one. I have a bunch of files with tags on the end like so... Filename {tag1}.ext Filename2 {tag1} {tag2}.ext I want to hold in a variable just the filename with all the " {tag}" removed. The tag can be anything so I'm looking... (4 Replies)
Discussion started by: kerppz
4 Replies

6. Shell Programming and Scripting

need help in string patterns

Hi, i have following lines of code which is properly working. CAT1="${InputFile}CAT_*0?????" CAT2="${InputFile}CAT_*0?????" CountRecords(){ integer i=1 while ]; do print P$i `nawk 'END {print NR}' $1 ` >> ${OutputPath}result.txt & i=i+1 shift done } CountRecords "$CAT1"... (8 Replies)
Discussion started by: malikshahid85
8 Replies

7. Shell Programming and Scripting

need help in string patterns

Hi, i have a directory /u02.i have 2 files in it like abc1.gz abc2.gz i want to store file pattern in a variable like f1="abc?" i don't want to take .gz in variable rather i want .gz appended when i need to unzip the file like gunzip $f1 Can you please help me how to... (3 Replies)
Discussion started by: malikshahid85
3 Replies

8. Shell Programming and Scripting

To extract the string between two patterns

Sample input: Loading File System Networking in nature Closing the System now i need to extract the patterns between the words File and Closing: i.e. sample output: System Networking in Nature Thanks in advance !!!!!!!!!!!!!!!!! (6 Replies)
Discussion started by: aajan
6 Replies

9. UNIX for Dummies Questions & Answers

Counting patterns in a shell string

Hello, I am writing a shell script and I need to find a way to count the number of whitespaces in a string. Eg: NAME="Bob Hope" I am looking for a way to count the number of whitespaces in this string. So a command that would take this string and return 1. Or take "First Middle Last"... (3 Replies)
Discussion started by: kevin80
3 Replies
Login or Register to Ask a Question