1832857 Members
3057 Online
110048 Solutions
New Discussion

Re: pattern match

 
Rahul_13
Advisor

pattern match

Hi ,

I have a file like:

1
abc
def
ghi
2
dhkhfk
hdfklhfl
hflhflf
1
nlltln
hlorn;f
nlahflh
3
b,xbx
nn.vn
nflhfl
4
ahff
jhflk

I need to grep for all the occurances for1 and print the lines that follows after it in a seprate file. Similiarly I have to do it for 2, 3, 4.

Can anyone please help me to do this.

Thanks,
Rahul
7 REPLIES 7
Francis_12
Trusted Contributor

Re: pattern match

Hello Rahul,

If each time the x sequence is followed by 3 lines, you may get a solution by using a small awk script combined with 'getline' which will force the read of next line each time.

Hope this helps, Bye.

Francis.
Rahul_13
Advisor

Re: pattern match

Hi Francis,

The number of line after x is not 3 but it is some "x" number which is fixed.

Can you please let me know how to do it.

Thanks,
Rahul
Sridhar Bhaskarla
Honored Contributor

Re: pattern match

Hi Rahul,

Not a great script. As long as the ID stuff looks like , then it will work. If you have some junk in place of , then you will see junk files getting created. So, try it in a test directory. May someone come up with a nicer one.

grep "" data|sort |uniq|while read i
do
FILE=$(echo "$i" |sed -e 's/>//g' -e 's/echo $FILE
sed -n -e '/'$FILE'<\/ID>/,//p' data |sed -e '/*/d' >> $FILE.out
done

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sundar_7
Honored Contributor

Re: pattern match

for NUM in 1 2 3 4
do
sed -n "/\$NUM\<\/ID\>/,/\.*<\/ID\>/p" 1 | grep -v ID > ID.$NUM.out
done

Better scripters are around. but hey, it works :-)
Learn What to do ,How to do and more importantly When to do ?
Ermin Borovac
Honored Contributor

Re: pattern match

Here is my attempt in perl. Lines that follow chunk will be written into chunk_.txt file.

#!/usr/bin/perl

open(FH, "< file") or die "$!";

# Slurp the whole file
undef $/;
$file = ;

# Separate file into chunks starting with
@chunks = split(/(?=\\d+\<\/ID\>)/, $file);

for $chunk (@chunks) {
# For each chunk extract its ID number and lines that follow
($id, $chunk) = ($chunk =~ /^\(\d+)\<\/ID\>\n(.+)$/sm);

# Append each chunk into a file called chunk_.txt
open(CHUNK, ">> chunk_${id}.txt") or die "$!";
print CHUNK "$chunk";
close(CHUNK);
}

close(FH);
Muthukumar_5
Honored Contributor

Re: pattern match

We can do with scripting as,

script

so that it will create inputfilename.

every file contains in between patterns of there.

#!/usr/bin/sh
index=0

# input file
testfile=$1

fun()
{
# Line
ln="$1"

if [[ $( echo $ln | grep '[0-9]' | wc -l ) -ne 1 ]]
then
echo $ln >> $testfile.$index
else
let index=index+1
fi

}

# main loop
while read line; do

fun $line

done < $testfile

# exit
exit 0


HTH.
Easy to suggest when don't know about the problem!

Re: pattern match

It appears to be an XML file. So it may be dangerous to assume that
"num" is on a single line.

Check out using XML::Twig module of perl