Hi experts,
I need a help in extracting a set of patterns from the text file.
Below is my scenario.
Input file:
I need to extract the data between
My output should be as
Thanks,
Kalai
Hi experts,
I need a help in extracting a set of patterns from the text file.
Below is my scenario.
Input file:
I need to extract the data between
My output should be as
Thanks,
Kalai
Assuming your whole file has the same format as your example :
awk 'NF>1{sub("\n",z,$NF);print $NF}' FS="input parameter {" RS="" infile
or
awk '/input parameter/{f=1;next}/}/{f=0}f&&/./' infile
$ 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
awk '/input parameter/{f=1;next}/}/{f=0}f&&/./' f1
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
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.
Output:
I need only the below output.
Thanks,
Kalai
# sed -n '/input parameter/{n;N;p;q}' infile
param1
param2
# awk '/input parameter/{getline;a=$0;getline;a=a"\n"$0;print a;exit}' infile
param1
param2
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..
-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
# 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(!(/}/||/[1]$/)) --> /}/ --> if the line not equal the "}"
|| --> OR
/[2]$/ --> empty line
then
print $0 --> print the record from input (your file)
else --> if not so is equals the its then exit
regards
ygemici
Note that ygemici's code
# awk '/input parameter/{getline;a=$0;getline;a=a"\n"$0;print a;exit}' infile
could be made more readable with :
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 ...)
awk '{t=/program input=/?1:/output/?0:t}t&&/input parameter/{f=1;next}/}/{f=0}f&&/./' infile
Thanks a lot experts..
i learned few thing from you...
Below code solved my requirement.