extracting lines based on condition and copy to another file

hi
i have an input file that contains some thing like this

aaa acc aa abc1 1232 aaa abc2....
poo awq aa abc1 aaa aaa abc2
bbb bcc bb abc1 3214 bbb abc3....
bab bbc bz abc1 3214 bbb abc3....
vvv ssa as  abc1 o09  aaa abc4....
azx aaq aa abc1 900 aqq abc19....
aaa aa aaaa abc1 899 aa abcZ....
mmm asa aa abc2 121 avv abc1....
mkm asa aa abc2 123 abb abc3....
m1m asa ua abc2 123 abb abc3....
poo awq aa abcX aaa aaa abc1....

abc[X,Y0-9] appears twice in each line. i want t0 get my output file some thing like this
file 1

aaa acc aa abc1 1232 aaa abc2....
poo awq aa abc1 aaa aaa abc2....

file2

bbb bcc bb abc1 3214 bbb abc3....
bab bbc bz abc1 3214 bbb abc3....

file#

mkm asa aa abc2 123 abb abc3....
m1m asa ua abc2 123 abb abc3....

and so on i,e., abc1 to abc[X,Y0-9] abc2 to abc[X,Y0-9] and so on till abcY to abc[X,Y0-9].

Try this:

awk 's != $4 FS $7 {s=$4 FS $7; c++}{print > "file" c}' infile

thankyou. but the field positions are not same always abc wont occur at 4/7 and more over with the code that was provided it is printing each line in seperate file i wanted to have all the line that have abc1 and abc2 in 1 file abc1 and abc 3 in another and so on

We are glad to help but if you post bad examples, you'll get wrong solutions.

Post realistic examples.

hi
my file contains lines which are not uniform in terms of their positions so instead of taking position into consideration its better to consider abc1 in this example and look for abc2,abc3 etc in the same line. all those which have abc1 and abc2 in same line have to be copied to seperate file and abc1 and abc3 to seperate so on.

aaa acc abc1 1232 aaa abc2....
poo awq aa  aaa abc1 aaa abc3
bbb bcc bb abc1 3214 bbb abc3....
file1
aaa acc abc1 1232 aaa abc2....
file2
poo awq aa abc1 aaa abc3....
bbb bcc bb abc1 3214 bbb abc3...

how about this?

grep "abc1.*abc2" yourinputfile > file1
grep "abc1.*abc3" yourinputfile > file2

Hi,

Try this out,

awk '/abc1.*abc2/{f="file2";}/abc1.*abc3/{f="file3";}f{print >f;}{f="";}' file

Cheers,
Ranga :slight_smile:

hi Ranga
its working. since i have abc11 also certain places while matching it gives me abc11 as well wen i just wanted abc1. while using grep i can give -w in case of awk i am not sure wat to do to get only abc1 not abc11 or abc12 or anythg else

awk '/ abc1 .* abc2 /{f="file2";}/ abc1 .* abc3 /{f="file3";}f{print >f;}{f="";}' file

Just put space around the pattern string.

Cheers,
Ranga :slight_smile: