I have a file that may have multiple instances of a string pattern (starting with X ending with Y). I want to extract each instance that matching this pattern. These may be on a single line or new line may or may not be at the beginning or end of a line. I have tried using grep with a regex pattern of (X.*Y) but the version of KornShell (ksh) that is running the ksh does not recognize the -c option so having a tough time extracting what I need. I have also read that awk can do this but not finding good documentation out there for awk.
Any help would be gladly appreciated.
If the patterns are contained on a single line, and only one instance on each line, you could try
awk -f ext.awk file
where file
is your input file and ext.awk
is
BEGIN {
pat="X.*Y"
}
$0 ~ pat {
match ($0,"(" pat ")",a)
print a[1]
}
If more than one pattern can be found on a line, or patterns extend over multiple lines, you can replace ext.awk
with:
{
line=line $0 RS
}
END {
while(match(line,/(X[^Y]*Y)/,a)) {
print a[1]
line=substr(line,RSTART+RLENGTH)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.