Extracting a set of patterns from the text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a set of patterns from the text file
# 1  
Old 03-23-2012
MySQL Extracting a set of patterns from the text file

Hi experts,

I need a help in extracting a set of patterns from the text file.

Below is my scenario.

Input file:
Quote:
input abc = Welcome;
program input= {
first input = ;
input parameter {
param1
param2

}
input param1 = {
Welcome to unix1
Welcome to unix2
}
input param2 = {
Welcome to unix3
Welcome to unix4
}
}
I need to extract the data between
Quote:
input param1 = { ------ }
My output should be as

Quote:
param1
param2

Thanks,
Kalai
# 2  
Old 03-23-2012
Assuming your whole file has the same format as your example :

Code:
awk 'NF>1{sub("\n",z,$NF);print $NF}' FS="input parameter {" RS="" infile

or
Code:
awk '/input parameter/{f=1;next}/}/{f=0}f&&/./' infile

Code:
$ cat f1
input abc = Welcome;
program input= {
first input = ;
input parameter {
param1
param2

}
input param1 = {
Welcome to unix1
Welcome to unix2
}
input param2 = {
Welcome to unix3
Welcome to unix4
}
}
$ awk 'NF>1{sub("\n",z,$NF);print $NF}' FS="input parameter {" RS="" f1
param1
param2
$ awk '/input parameter/{f=1;next}/}/{f=0}f&&/./' f1
param1
param2

Code:
awk '/input parameter/{f=1;next}/}/{f=0}f&&/./' f1


Last edited by ctsgnb; 03-23-2012 at 05:56 PM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-24-2012
MySQL

wow.. thanks a lot..
can you please explain me your three code. It will help me to learn awk more better.

Thanks,
Kalai

---------- Post updated at 11:36 AM ---------- Previous update was at 09:24 AM ----------

Hi Experts,

i came across one more scenario.Initially i have not thought about it.

Below is my input

Quote:
input abc = Welcome;
program input= {
first input = ;
input parameter {
param1
param2

}
input param1 = {
Welcome to unix1
Welcome to unix2
}
input param2 = {
Welcome to unix3
Welcome to unix4
}
}

output= {

input parameter {
out-param1
out-param2

}
}
I want to extract my pattern "input parameter" from the block "program input".

When i run the below command i get the out put is.



Quote:
awk '/input parameter/{f=1;next}/}/{f=0}f&&/./' f1
Output:

Quote:
param1
param2
out-param1
out-param2
I need only the below output.
Quote:
param1
param2
Thanks,
Kalai
# 4  
Old 03-24-2012
Code:
# sed -n '/input parameter/{n;N;p;q}' infile
param1
param2

Code:
# awk '/input parameter/{getline;a=$0;getline;a=a"\n"$0;print a;exit}' infile
param1
param2

# 5  
Old 03-24-2012
can u please explain me how it works. it will be very helpfull
# 6  
Old 03-24-2012
Quote:
Originally Posted by kalpeer
can u please explain me how it works. it will be very helpfull
/* sed */
we use the "-n" parameter for only print the line when our pattern matches with input..
Code:
-n, --quiet, --silent
              suppress automatic printing of pattern space

you can think like so ;

/input parameter/ --> we find the our pattern..and then (if find the our pattern then process our commands..)
{n;N;p;q} -->
n: read the next line ("param1" --> our pattern space)
N: append the next line to our pattern space ("param1"\n"param2")
p: print the pattern space ("param1"\n"param2")
q: quit


/* awk */
/input parameter/ --> we find the our pattern..and then (if find the our pattern then process the our commands..)
getline : bring the next line ("param1")
a=$0 (a="param1")
getline : bring the next line ("param2")
a=a"\n"$0 (a="param1"\n"param2")
print and exit


and an extra for you Smilie
Code:
# awk '/input parameter/{while(getline){if(!(/}/||/^[\t ]*$/)) print $0;else exit}}' infile
param1
param2

/* awk 2 */

/input parameter/ --> same the above explanation
while(getline) --> while read and bring next line from input(s) (if there isnt an access/permission/read problem,awk read while it comes the EOF)
if(!(/}/||/^[\t ]*$/)) --> /}/ --> if the line not equal the "}"
|| --> OR
/^[\t ]*$/ --> empty line
then
print $0 --> print the record from input (your file)
else --> if not so is equals the its then exit



regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 7  
Old 03-24-2012
Note that ygemici's code
Code:
# awk '/input parameter/{getline;a=$0;getline;a=a"\n"$0;print a;exit}' infile

could be made more readable with :

Code:
awk '/input parameter/{getline;print;getline;print;exit}' infile

Be aware that these codes assume
1) that your program always have exactly 2 input parameters
2) that these parameter are never separated with one or more empty lines
So ... it may - or not - be too restrictive ... depending on your requirement.

Just another one which should do the work (even if you have many input parameter and even if they are separated with one or more empty lines, and also if your input file contains a succession of input and output ...)
Code:
awk '{t=/program input=/?1:/output/?0:t}t&&/input parameter/{f=1;next}/}/{f=0}f&&/./' infile


Last edited by ctsgnb; 03-24-2012 at 05:49 PM..
This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using grep to select specific patterns in text file?

How do I use grep to select words that start with I or O, end in box, and contain at least one letter in between them? the text file mailinfo.txt contains Inbox the Inbox Is a match box Doesn't match INBOX Outbox Outbox1 InbOX Ibox I box If the command works correctly it... (4 Replies)
Discussion started by: steezuschrist96
4 Replies

2. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

3. Shell Programming and Scripting

help extracting text from file

Hello I have a large file with lines beginning with 552, 553, 554, below is a small sample, I need to extract the data you can see below highlighted in bold from this file on the same location on every line and output it to a new file. Thank you in advance for any help 55201KL... (2 Replies)
Discussion started by: firefox2k2
2 Replies

4. Shell Programming and Scripting

Help with extracting a part of a line between two patterns

Hello All, I have a text file with contents as below: contents of error.txt: message1="Reason for error code1" message2="Reason for error code2" message3="Reason for error code3. To solve this, you may try doing restart" I have a requirement where in I have to... (4 Replies)
Discussion started by: asterisk-ix_use
4 Replies

5. Shell Programming and Scripting

Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file

Hi All, I have a small query. I have a file containing the following lines File 1: 29-Jul-2011 GMT Static data requires update <Extraction should start here> ----------- ----------- -------------------- ----------------------- ----------- <should stop here> Pattern1 will be time... (2 Replies)
Discussion started by: gangii87
2 Replies

6. UNIX for Dummies Questions & Answers

Locating and Extracting Specific Patterns from a file

Hi all, 1. I have a file that is getting continously refreshed (appended) I want to grep all the strings containing substring of the type abcdf123@aaa.xxx.yyy.zzz:portnumber: where, before @, any letters or numbers combination, after @, IP address then symbol : then port... (4 Replies)
Discussion started by: kokoras
4 Replies

7. Shell Programming and Scripting

Perl - How to search a text file with multiple patterns?

Good day, great gurus, I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Discussion started by: Sp3ck
2 Replies

8. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies

9. Shell Programming and Scripting

extracting a set of strings from a text file

i have textfiles that contain a series of lines that look like this: string0 .................................................... column3a column4a string1**384y0439 ..................................... column3b column4b... (2 Replies)
Discussion started by: Deanne
2 Replies

10. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies
Login or Register to Ask a Question