how to retrieve required specific info from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to retrieve required specific info from the file
# 1  
Old 06-25-2008
how to retrieve required specific info from the file

Hi
I have a file which consists of a number in the square brackets, followed by the blank line, then several lines which describe this number. This pattern is repeated several thousands time. The number in the brackets and the decription of it is unique. For example:
[2153]

ASRVSERV=1241GD;
BPCONN=avtr213;
OMGEO=FIET

[4324]

ASRVSERV=333TS;
BPCONN=AUTO1243;
OMGEO=TSAM

etc....................

Is there a way to retireve info from the above file for several specific numbers and output it to another file?
I do not keep my hopes high, but may be somebody did similar file manipulations. Thanks a lot for any help or hints -A

PS I am in ksh88
# 2  
Old 06-26-2008
Code:
awk  -v RS=    '/\[2153\]|\[4324\]|\[9999\]/ { a=NR }  NR==a, NR==a+1'  ORS="\n\n"   filename  >  output

Add as many numbers as you need to search, with no spaces in between and separated from the pipe | symbol, as shown above.
# 3  
Old 06-26-2008
Basically you are trying to parse multi-line record.
See my blog for an example:
Chi Hung Chan: How to Parse Multiline in AWK

Below should do the job
Code:
$ cat d.sh
#! /bin/sh

if [ $# -ne 2 ]; then
        echo "Usage: $0 <input> <pattern>"
        exit 1
fi


awk '
BEGIN {
        RS=""
        FS="\n"
}
NR%2==1 && $1 ~ /\['$2'\]/ {
        getline
        print
}' $1

$ cat d.in
[2153]

ASRVSERV=1241GD;
BPCONN=avtr213;
OMGEO=FIET

[4324]

ASRVSERV=333TS;
BPCONN=AUTO1243;
OMGEO=TSAM

[4858]

ASRVSERV=345RE;
BPCONN=ABC123;
OMGEO=MSAM

[8888]

ASRVSERV=899AS;
BPCONN=CAR1234;
OMGEO=IAOS

[8912]

ASRVSERV=728WW;
BPCONN=POP1239;
OMGEO=JKAE

[1211]

ASRVSERV=999UI;
BPCONN=LOC1932;
OMGEO=UISE

$ ./d.sh d.in 4324
ASRVSERV=333TS;
BPCONN=AUTO1243;
OMGEO=TSAM

$ ./d.sh d.in 8912
ASRVSERV=728WW;
BPCONN=POP1239;
OMGEO=JKAE

$ ./d.sh d.in 1111

$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

2. Shell Programming and Scripting

Help required deleting specific lines from file

Hi, I have a file with 20 columns of data and hundreds of lines of the same format. Here is an example line. The data repeats underneath with the same format. 15 1 4 GLY - 1 65 LYSH 23 N - 24 H - 634 O 0.188 157.552 487 48.70I have been sorting this data by hand but I was wondering if I... (3 Replies)
Discussion started by: livbaddeley
3 Replies

3. Shell Programming and Scripting

Perl script to get info from specific rows & columns (.xls file)

Hi all, I want to read some specific rows & columns in the .xls file with my script to get the data to be manipulated. Now, I can read the .xls file correctly & i can go to the specific sheet that I want but i have a problem to specify the specific rows & columns. I mean, I want to get the info... (0 Replies)
Discussion started by: Yohannita
0 Replies

4. Shell Programming and Scripting

How to retrieve a file from specific path using unix script?

Hi i'm new to shell script, i want to get the filename from specific location which i mentioned in my script. The scirpt should read the filename exactly using the following command "ls -ltr | tail -1". Could someone show me on this. Below is my script #!/bin/ksh PATH= /usr/ if then ... (4 Replies)
Discussion started by: fresher
4 Replies

5. Solaris

prstat info required

Hi, I issued the following command on my solaris 10 and got the following info. prstat -Z PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP 1344 oracle 4872M 2392M sleep 59 0 145:49:50 0.1% oracle/1 17523 oracle 3976K 3672K cpu18 59 0 0:00:04 0.1% prstat/1 1550 oracle 3276M... (1 Reply)
Discussion started by: malikshahid85
1 Replies

6. Shell Programming and Scripting

Script required to extract a specific snippet from the entire file.

Hi, I have a file with the following structure. XXXXX........... YYYYY........... ................. .................. ZZZZZZ...... qwerty_start.............. .................. ................. .................. querty_end................ .............................. (1 Reply)
Discussion started by: abinash
1 Replies

7. Programming

How can i retrieve some specific lines from a file using C

Plz tel me how to retrieve some specific set of lines from a file and store it in a char buffer.I am seperating each record by ":" 22:abc:4 hardware:cd:xyz:2 hardware:eth:abc:6 hardware:mouse:xyz:3 hardware:ram:xyz:1 23:cde:3 hardware:cd:xyz:2 hardware:eth:abc:6 hardware:ram:xyz:1 ... (3 Replies)
Discussion started by: vigneshinbox
3 Replies

8. UNIX for Advanced & Expert Users

Required info on Pstack on solaris10

Hi All , I am wanted to know the #of thread ( Kernal & User ) created by the Process i was looking into the prstat PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP 994 httpd 126M 99M sleep 58 0 1:11.25 2.4% ns-httpd/33 997 httpd 114M 86M sleep 58 0 0:40.02 2.7% ns-httpd/22 995... (0 Replies)
Discussion started by: rajendra44
0 Replies

9. UNIX for Dummies Questions & Answers

(cont) Retrieve line from a file based on a value in specific column

HI, Your help was great: awk -F":" '$5 ~ /^P/{print }' file I would like to know what changes need to be done to this line code, so that I can put it in a shell script and call it as the example below. example: countries that start with chacater 'P' > country P Result: ... (0 Replies)
Discussion started by: efernandes
0 Replies

10. UNIX for Dummies Questions & Answers

Retrieve line from a file based on a value in specific column

Hi, I have a file that has several values seperated by ":" 2006:John:Student:Football:Portugal:Cinema 2006:James:Engineer:Basket:Poland:Theatre 2007:Lucy:Diver:Gymnastic:England:Music 2007:Smith:Plumber:Basket:Spain:Poker I need make a filter based on the 5th field to find countries that... (1 Reply)
Discussion started by: efernandes
1 Replies
Login or Register to Ask a Question