Pattern search and save it as .txt file with some name..

Hello,
I have a note pad at /usr/abc location with the following content, since it is a huge file i need to split it into multiple .txt files.

A123|akdhj      |21kjsdff  |b212b1b21 |0
A123asdasd     |assdd     |asdasdsdqw|6
A123|QEWQ    |NMTGHJK |zxczxczx|3
A123|GEGBGH  |RTYBN    |xcvvxc|2
A123|VDFGER  |RYRYRTY |QRRWR|6
A123|RGEDFB   |YRTYTR |DFGDGDG|1
A123|DFRER    |WT3YTY |DGDGFG|2
A123|DFBDB    |QRQR     |QRQRQ|6
A123|DBDB     |WEWER   |QQQWER|8
A123|DBDBB   |WERWER  |YJTYJTY|0
Z213             |fasf        |afaf|afaffa|23
Z213             |asf        |afaf|afaffa|0
Z213             |FDDS        |afaf|afaffa|89
Z213             |RWER       |afaf|afaffa|8
Z213             |fQWE        |afaf|afaffa|8
Z213             |sdfdsf        |afaf|afaffa|7
Z213             |bfgh        |afaf|afaffa|3
Z213             |erter        |afaf|afaffa|1
Z213             |tyutyu        |afaf|afaffa|3
Z213             |rtret        |afaf|afaffa|9
Z213             |werwe        |afaf|afaffa|1

Above is the ac.txt file with huge content like this some starting with A123 some with Z213 and so on...
Now my requirement.

I want to read this file first search for A123 and save it in some other location containg all the records of A123, then search for Z213 and save it in some other location containg all the records of Z213 and so on..
Please suggest.

for i in A213 Z213
do
 grep $i ac.txt > /tmp/$i.txt
done

Try this.

Few assumptions made,

  1. First four chars of each line is taken as the matcing pattern
  2. output file will be created with matching pattern.txt format
awk  '{print $0 >>substr($0,0,4)".txt"}'  test.txt
awk -F'[ |]' '{print > $1".txt"}' inputfile

What about second row of the sample input ?

Oops!! I didn notice that precise..

awk '{print > substr($0,0,4)".txt"}' inputfile

Ok, i will try and will comment.
But i hope the above all solution is searching the pattern and then copying all the contents starting with A123 and putting it in some txt file and again searching pattern "Z213 " and again putting it in some other txt file.

Also i hope its copying the whole content starting with A123 and Z213; and not only A123 and Z213.

You are right, and above code will also work for any pattern like A123 and Z213.

Mind blowing @kumaran_555...it happened.Can you please explain how script in short.
Its creating separate pattern.txt files. Now One more requirement is i need to divide any of the .txt created files into 4 files dividing the contents of the main file.

For Example as soon as the A123.txt file is created, since the size for this txt file is too large i want to divide this file into 4 files with any name..

man split

After the awk command, execute the below script. Try to avoid the main file name in the list

for f in `ls|grep -v main_file|xargs`
do
line=`wc -l $f`|cut -d' ' -f1`
line_per_file=`echo $line/4|bc`
split -l $line_per_file $f $f
done

@Kumaran_555 your below code for searching pattern and making one file per pattern is absolutely working fine..
your code

awk  '{print $0 >>substr($0,0,4)".txt"}'  test.txt

now what is this doing is, if i have 20 different pattern like A123,Z213,O23,P098 and so on.it create files for all the pattern.

now if i want only 2 pattern out of these say A123 and O23, how can this be possible...

Can anybody suggest ???

---------- Post updated at 02:57 PM ---------- Previous update was at 11:55 AM ----------

@Kumaran_555 your below code for searching pattern and making one file per pattern is absolutely working fine..
your code

awk '{print $0 >>substr($0,0,4)".txt"}' test.txt
 

now what is this doing is, if i have 20 different pattern like A123,Z213,O23,P098 and so on.it create files for all the pattern.

now if i want only 2 pattern out of these say A123 and O23, how can this be possible...

---------- Post updated 07-19-11 at 11:25 AM ---------- Previous update was 07-18-11 at 02:57 PM ----------

Can anybody look into this ???

Please try to state your problem/requirement in a single post. Adding new requirement to each post may not comply with the subject line.

awk '/A123|O23/{print $0 >substr($0,0,4)".txt"}' test.txt

OK. i will make sure not to continue a new requirement in the old post.
@michaelrozar17 - Its working but partially...
output of the above command retrives all the pattern starting with A, means its returning A123, A909 or anything starting with A in test.txt file.

I want to find only 2 pattern A123 and O23...

---------- Post updated at 06:14 PM ---------- Previous update was at 04:25 PM ----------

The above command is all displaying the pattern starting with A which i dont want.I only want A123 and O23...

If it still does not work with the below command as required, please post the input file your trying with..

awk '/^A123|^O23/{print $0 >substr($0,0,4)".txt"}' test.txt