Search pattern and write line into another file

Hi,

I have a file which contains the below details.. My requirement is to fetch all the lines which are starting with "ABC_XY_" into 1 file and rest of the lines (not starting with "ABC_XY_") into another file.

Could you please help with what command needs to be used?
file1.txt
----------

ABC_XY_1234,John
ABC_XY_2345,Raj
ABC_XY_769536, Mark
ABC_XY_82758,George
ABC_XY_8643969,Kate
ABC_XY_93465, Nat
ABC_XY_3456846, Bush
ABC_XY_6907640,King
ABC_XY_5482582,Young
ABC_XY_567,George 
PQR_85493,Young
PQR_564,kate
PQR_422156,Mark
PQR_2565667, Joker
PQR_35235, Philips
PQR_3535, Glen
PQR_53535,Rossel
PQR_23438, Mark
PQR_436465, Tony

Expected o/p:
-------------------

out1.txt
-------
ABC_XY_1234,John
ABC_XY_2345,Raj
ABC_XY_769536, Mark
ABC_XY_82758,George
ABC_XY_8643969,Kate
ABC_XY_93465, Nat
ABC_XY_3456846, Bush
ABC_XY_6907640,King
ABC_XY_5482582,Young
ABC_XY_567,George 

Out2.txt
--------
PQR_85493,Young
PQR_564,kate
PQR_422156,Mark
PQR_2565667, Joker
PQR_35235, Philips
PQR_3535, Glen
PQR_53535,Rossel
PQR_23438, Mark
PQR_436465, Tony

:b:Cheers,
Satya

Use codetag

Try

$ awk -F'[_]' 'NR==1 || p!~$1{close(f);f="Out"++i".txt"}{print $0>f;p=$1}' file
$ ls Out* -1 
Out1.txt
Out2.txt

Could you please explain the functionality of the above code..

My requirement is to search for a string in each line and print that line if it starts with ABC_XY_*
And those are not start with ABC_XY_* they will be written into another file.

My source file which I am reading is file1.txt

Thanks for your quick response.

Hi there, very elegant solution just tried it on my Android tablet and it worked apart from the output file names which came out as 1.text and 2.text ? Any idea why ?

Regards

create a script test.sh =

cat test.sh contains below commands -

awk '($1 ~ /ABC_XY_/){print $0}' file1.txt > out1.txt
awk '($1 !~ /ABC_XY_/){print $0}' file2.txt > out2.txt

Both input files should be file1.text :slight_smile:

As user requested in #1 it generates o/p like this

$ cat Out1.txt 
ABC_XY_1234,John
ABC_XY_2345,Raj
ABC_XY_769536, Mark
ABC_XY_82758,George
ABC_XY_8643969,Kate
ABC_XY_93465, Nat
ABC_XY_3456846, Bush
ABC_XY_6907640,King
ABC_XY_5482582,Young
ABC_XY_567,George
$ cat Out2.txt 
PQR_85493,Young
PQR_564,kate
PQR_422156,Mark
PQR_2565667, Joker
PQR_35235, Philips
PQR_3535, Glen
PQR_53535,Rossel
PQR_23438, Mark
PQR_436465, Tony

Tested on $

awk --v
GNU Awk 3.1.8
awk '{f="out" (/^ABC_XY_/?1:2) ".txt";print $0 > f }' infile

Satya, if you wanted to create a separate file for each group ( which could be useful ) try this

awk -F"_" '{print $0 > $1".txt"}' file1.txt

---------- Post updated at 11:25 AM ---------- Previous update was at 10:59 AM ----------

The output is fine but on my system the files created are 1.text and 2.text and I don't know why

Which awk you are using ? which OS ?

If you used below one it will create file name Out1.txt,Out2.txt....Outn.txt

if suppose by mistake if you made f="Out"++i".txt" to f=++i".txt" It will create 1.txt,2.txt.....n.txt

Hi Ashkay, I didn't make a mistake I cut and pasted your code, I'm using Android and have found out what I need to do is


awk -F'[_]' 'NR==1 || p!~$1{close(f);f=("Out")++i".txt"}{print $0>f;p=$1}' file
# note the parenthesis around "Out".

Must be an Android quirk , anyway a lesson learnt. Thanks for your time.

I have no Idea about android

Ooh I like/this one !!! Very elegant and tight.

---------- Post updated at 12:21 PM ---------- Previous update was at 12:18 PM ----------

As Android is based on Linux/Unix I thought it would be consistent. ( silly me )