searching for info in paragraph


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching for info in paragraph
# 1  
Old 10-23-2008
searching for info in paragraph

I need to capture data from a paragraph list and am not sure how to get my data. I created a file from a tapeutil command which has many entries 1 entry is posted below
;
Slot Address 1025
Slot State ..................... Normal
ASC/ASCQ ....................... 0000
Media Present .................. Yes
Robot Access Allowed ........... Yes
Source Element Address Valid ... No
Media Inverted ................. No
Volume Tag ..................... 00001

I want to search for the volume tag # 00001 but I want to capture the Slot Address # 1025. Basically I want to search a file for tape 0001 and get a return of Slot Address 1025.
# 2  
Old 10-23-2008
Question what do records 1 & 3 look like?

Do they follow the same pattern? As in...

Slot Address 1024
Slot State ..................... Normal
ASC/ASCQ ....................... 0000
Media Present .................. Yes
Robot Access Allowed ........... Yes
Source Element Address Valid ... No
Media Inverted ................. No
Volume Tag ..................... 00011
Slot Address 1025
Slot State ..................... Normal
ASC/ASCQ ....................... 0000
Media Present .................. Yes
Robot Access Allowed ........... Yes
Source Element Address Valid ... No
Media Inverted ................. No
Volume Tag ..................... 00001

Slot Address 1026
Slot State ..................... Normal
ASC/ASCQ ....................... 0000
Media Present .................. Yes
Robot Access Allowed ........... Yes
Source Element Address Valid ... No
Media Inverted ................. No
Volume Tag ..................... 00002
# 3  
Old 10-23-2008
Quote:
Originally Posted by daveisme
...
Slot Address 1025
Slot State ..................... Normal
ASC/ASCQ ....................... 0000
Media Present .................. Yes
Robot Access Allowed ........... Yes
Source Element Address Valid ... No
Media Inverted ................. No
Volume Tag ..................... 00001

...Basically I want to search a file for tape 0001 and get a return of Slot Address 1025.

Code:
tag=00001

awk -v t="$tag" '/Volume Tag/ && $NF==t {print s} /Slot Address/ {s=$0}' file

Output

Code:
Slot Address 1025

# 4  
Old 10-24-2008
follow up for joey

Yes the records look just as you specified. The follow the same pattern.
# 5  
Old 10-24-2008
Hammer & Screwdriver OK, here goes

Code:
> cat file301
Slot Address 1024
Slot State ..................... Normal
ASC/ASCQ ....................... 0000
Media Present .................. Yes
Robot Access Allowed ........... Yes
Source Element Address Valid ... No
Media Inverted ................. No
Volume Tag ..................... 00011
Slot Address 1025
Slot State ..................... Normal
ASC/ASCQ ....................... 0000
Media Present .................. Yes
Robot Access Allowed ........... Yes
Source Element Address Valid ... No
Media Inverted ................. No
Volume Tag ..................... 00001
Slot Address 1026
Slot State ..................... Normal
ASC/ASCQ ....................... 0000
Media Present .................. Yes
Robot Access Allowed ........... Yes
Source Element Address Valid ... No
Media Inverted ................. No
Volume Tag ..................... 00002

> cat file301 | sed "s/^Slot Address/~&/" | tr -s "." | tr "\n" " " | tr "~" "\n" | grep "Volume Tag . 00001" | cut -d " " -f3
1025

> cat file301 | sed "s/^Slot Address/~&/" | tr -s "." | tr "\n" " " | tr "~" "\n" | grep "Volume Tag . 00011" | cut -d " " -f3
1024

> cat file301 | sed "s/^Slot Address/~&/" | tr -s "." | tr "\n" " " | tr "~" "\n" | grep "Volume Tag . 00002" | cut -d " " -f3
1026

>

My logic explained:
want to place a '~' as a record marker
get rid of all those extra '.' characters
get rid of new-line characters (one big run-on record now)
turn the '~' into new-line characters (to again separate records)
grep for the desired text - note only one '.'
take the third field
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract paragraph that contains a value x<-30

I am using OSX. I have a multi-mol2 file (text file with coordinates and info for several molecules). An example of two molecules in the file is given below for molecule1 and molecule 2. The total file contains >50,000 molecules. I would like to extract out and write to another file only the... (2 Replies)
Discussion started by: Egy
2 Replies

2. Shell Programming and Scripting

Compare a paragraph

I know this is a challenging, its about comparing a 3 paragraph, from a whois command, i want to print what is the data that is not unique, example below is the admin phone and techphone, the rest is the same.it will print the correct and wrong # whois google.com| sed -n '/Registry Registrant... (3 Replies)
Discussion started by: kenshinhimura
3 Replies

3. Shell Programming and Scripting

How to grep paragraph?

Hi, I have A file like this: >Contig1 AAAAAAATTTTTTCCCAATATATGAT ATATATAEATATATAT >Contig2 AAAAAAATTTTTTCCCAATATATGAT ATATATAEAATTTTTAATTTTTTCCCA ATCCCAAATATATAT >Contig3 AAAAAAATTTTTTCCCAATATATGAT ATATATAEAATTTTTAATTTTTTCCCA ATCCCAAATAAATTTTTTCCCAATAT ATGATATATATAEAATTTTTAATTTTT... (3 Replies)
Discussion started by: the_simpsons
3 Replies

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

5. Shell Programming and Scripting

Pattern in a paragraph

Hi, I have read similar questions and I was not able to get a solution, so please help :) I have two files: 1-Pattern file contains list of patterns pattern1 pattern2 pattern3 pattern4 .... pattern# 2-input file in the format >hdhfshsdfjhpattern1xmbxmbxnmv... (4 Replies)
Discussion started by: tbakheet
4 Replies

6. UNIX for Dummies Questions & Answers

Split a paragraph

Hi, Consider the following paragraph. This is line1. This is line2, This is last line. I need the output as 4:This is last line. i.e The line after the blank line should be displayed along with line number. I am a unix begineer.Any one please help me to solve this problem (3 Replies)
Discussion started by: Sekar1
3 Replies

7. Linux

Extract a paragraph

Hi , Unix.com has been life saver for me I admit :) I am trying to extract a paragraph based on matching pattern "CREATE TABLE " from a ddl file . The paragraphs are seperated by blank line . Input file is #cat zip.20080604.sql1 CONNECT TO TST103 SET SESSION_USER OPSDM002 ... (2 Replies)
Discussion started by: capri_drm
2 Replies

8. Shell Programming and Scripting

help, using awk to get paragraph

Hi, I'm trying to find pattern in log file. I'm using awk to search the pattern and print the paragraph. And it's not working well like I want. The file actually is TCAP message, it has format like this : ... ... ... ***************************************** INCOMING TCAP MESSAGE At... (6 Replies)
Discussion started by: kunimi
6 Replies

9. Shell Programming and Scripting

extracting last paragraph from a text

Hi All, I want to extract last paragraph from a text for example aaaaa sadafsa sdfdsaf asfdsad asfdsfad asfdfsafd adfssas asdfsafdsa asfdasdfa sadfasdsaf i need the last paragraph how to do it using linux commands (1 Reply)
Discussion started by: uvrakesh
1 Replies

10. Shell Programming and Scripting

Bold the paragraph

Hi, I have a file with multiple paragraph. I want to look for some word and make that paragraph bold. How can I do that? Thanks, Karthik (3 Replies)
Discussion started by: caprikar
3 Replies
Login or Register to Ask a Question