extract specific line if the search pattern is found


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract specific line if the search pattern is found
# 1  
extract specific line if the search pattern is found

Hi,

I need to extract <APPNUMBER> tag alone, if the <college> haas IIT Chennai value. college tag value will have spaces embedded. Those spaces should not be suppresses.

My Source file

Code:
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh   suderam</name><college>IIT                    Chennai   </college>
<details>
<interviewheldby>Raja</<interviewheldby>
<courseopted>MSC</courseopted>
<Regno>742</Regno>
<BuildingName>Rajvilas</BuildingName>
<CustomerClass>B</CustomerClass>
<Appnumber>3018369</Appnumber>
<Date>21/05/2010 06:04:22</Date>
<PostalOutcode>N8</PostalOutcode>
<PostalIncode>9SJ</PostalIncode>
</details>
</Record>
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh   suderam</name><placeofbirth>chennai     mylapore   </placeofbirth><dateofbirth>22ndmay99</dateofbirth><college>IIT                    Chennai   </college><course>MCA</course>
<details>
<interviewheldby>Suresh</<interviewheldby>
<courseopted>MSC</courseopted>
<Regno>742</Regno>
<BuildingName>Rajvilas</BuildingName>
<CustomerClass>B</CustomerClass>
<Date>21/05/2010 06:04:22</Date>
<Appnumber>3018370</Appnumber>
<PostalOutcode>N8</PostalOutcode>
<PostalIncode>9SJ</PostalIncode>
</details>
</Record>
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh   suderam</name><placeofbirth>chennai     mylapore   </placeofbirth><dateofbirth>22ndmay99</dateofbirth><college>IIT                    Mumbai   </college><course>MCA</course>
<details>
<interviewheldby>Suresh</<interviewheldby>
<courseopted>MSC</courseopted>
<Regno>742</Regno>
<BuildingName>Rajvilas</BuildingName>
<CustomerClass>B</CustomerClass>
<Appnumber>3018370</Appnumber>
<Date>21/05/2010 06:04:22</Date>
<PostalOutcode>N8</PostalOutcode>
<PostalIncode>9SJ</PostalIncode>
<Appnumber>3018370</Appnumber>
</details>
</Record>

Please guide me.
# 2  
You can try:
Code:
awk -F"[<>]" '
/<Record>/ && /IIT/ && /Chennai/{f=1}
f && /<Appnumber>/{print $3; f=0}
' file

# 3  
cannot test,
your source file is not well formed.
# 4  
Thanks for your response.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #778
Difficulty: Medium
Harmonic analysis is a branch of mathematics concerned with the representation of functions or signals as the superposition of basic waves.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file. abc.txt nas1:/abc/test/test1 /test nas1:/abc/test/test1/test2 /test/abc nas1:/abc/test/ Now i have a variable that contains "nas1:/abc/test/test1" value , so i need to search the above file for this variable and print only this line. ... (14 Replies)
Discussion started by: mohit_vardhani
14 Replies

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

4. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

5. Shell Programming and Scripting

Breaking the sum of a column when specific pattern is found

Hi Am trying to sum up a file # cat /Out maths 2 0.0 english 2091 0.2 history -1 0.0 physics 18 0.0 -------------------------------------- maths ... (9 Replies)
Discussion started by: Priya Amaresh
9 Replies

6. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

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

8. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

10. Shell Programming and Scripting

Need to extract specific pattern from logfile

Log File: Attempting to contact (DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname1.com)(PORT=1521)))(CONNECT_DATA=(SID=database1)(SRVR=DEDICATED))) Attempting to contact... (2 Replies)
Discussion started by: techychap
2 Replies

Featured Tech Videos