Visit Our UNIX and Linux User Community


How to grep multiple pattern from XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep multiple pattern from XML file
# 1  
Old 04-07-2010
Error How to grep multiple pattern from XML file

Hi Everyone
pls if anyone can help me in writing a script or correcting it what I have done.

I want to write a script to grep record number for all those record which have abc xyd cat dog in one of the field say VALUE, I have thousand of file in a dir and I want to search every file for mathching pattern in field VALUE ,I was trying to give path of dir from command prompt.
Code:
#!/bin/sh
filePath=$1;
for i in `ls $filepath`
do
  for y in `grep -il patternfile $filepath$i`
  do
    echo "processing file $y"
    egrep "<RecordNumber>" $y >>metafile.txt
  done
done

Thanks

Last edited by Franklin52; 04-07-2010 at 04:17 AM.. Reason: Please indent your code and use code tags!
# 2  
Old 04-07-2010
What is the error you face in this ?

Or what stops you from achieving wht you require ?
# 3  
Old 04-07-2010
Network The error I am Getting

My script is not searching in every file in a dir for the multiple pattern which I have saved to a pattern file. Is there a another way to solve the problem . I think I am doing something wrong in looping . It is not searching everyfile in a dir.
pls help me if anyone solve this or figure it out.

Thanks
# 4  
Old 04-07-2010
correct the capital P in filePath in line 2

put a / between $filepath and $i in line 5 - unless you always give param 1 with a trailing /

You could also try ls -1 to make sure the filenames come out on separate lines.
# 5  
Old 04-07-2010
You need to find those files which include something and if include then find some other line ?

You can use filegeneration, no need to use ls.

But in this case, you can use grep, no need to make fileloop.
Code:
filepath="$1"
grep -il "VALUE"  $filepath/* | while read file
do
    grep "<RecordNumber>" $file >>metafile.txt
done

If you need also files begin ., then something like
Code:
grep ... $filepath/* $filepath/.[!.]*  | ...

If you need more rule (=OR)
then
grep -il -e "pattern1" -e "pattern2"
or write rule to the file and then
grep -il -f patternfile ...

If you need AND patterns, then you need to pipe grep commands.

Example input data and example output data is the best method to tell us your real needs.

Last edited by kshji; 04-07-2010 at 12:16 PM..
# 6  
Old 04-07-2010
Network I want the record no for specific field where the pattern occur

Thanks all for replying back
my files(1000 of them ) in dir. My data lin file is ike this

<meta:term-type>PUB_SEG_TYPE</meta:term-type>
<meta:term-source>Atlas</meta:term-source>
<meta:term-id>14242630</meta:term-id>
<meta:term-value>Article</meta:term-value>

I want to get all the record number for those record where <meta:term-source> is list, table, abc, photograph.

Thanks
# 7  
Old 04-07-2010
Do you mean some thing like this:

Code:
 
awk '/<meta:term-source>/' * | egrep "list|table|abc|photograph"


Previous Thread | Next Thread
Test Your Knowledge in Computers #496
Difficulty: Easy
Many programming languages assign special meaning to keywords such as for, if, and while (for example) that are used to define various control structures.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

3. UNIX for Advanced & Expert Users

Grep a pattern from zip file which has multiple files insdie

Hi Gurus, I got a small requirement in my script to grep a specific pattern in a zip compressed file which has been created with multiple files. Sample File: 20180913.zip $> zipinfo -l 20180913.zip 20180913_file1 20180913_file2 20180913_file3 20180912_file4 20180912_file5... (1 Reply)
Discussion started by: hi.villinda
1 Replies

4. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

5. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

6. Shell Programming and Scripting

Grep multiple pattern

I have got a text from each line, I need to fetch port only if there is an ip a.text text and port=25 b.ip=(12.32.54.256) and port="52" c.ip=(55.251.253.12) and port=25" d.text text and port="5" e.ip=(45.211.155.15) and port="457" f.ip=(144.158.256.2) and port="588" I know how to... (6 Replies)
Discussion started by: arpagon
6 Replies

7. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

8. 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

9. Shell Programming and Scripting

split XML file into multiple files based on pattern

Hello, I am using awk to split a file into multiple files using command: nawk '{ if ( $1 == "<process" ) { n=split($2, arr, "\""); file=arr } print > file }' processes.xml <process name="Process1.process"> ... (3 Replies)
Discussion started by: chiru_h
3 Replies

10. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

Featured Tech Videos