Search pattern and write line into another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search pattern and write line into another file
# 1  
Old 10-10-2013
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
----------
Code:
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:
-------------------
Code:
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

SmilieCheers,
Satya

Last edited by Franklin52; 10-10-2013 at 04:37 AM.. Reason: Please use code tags
# 2  
Old 10-10-2013
Use codetag

Try

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

Code:
$ ls Out* -1 
Out1.txt
Out2.txt

# 3  
Old 10-10-2013
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.
# 4  
Old 10-10-2013
Quote:
Originally Posted by Akshay Hegde
Use codetag

Try

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

Code:
$ ls Out* -1 
Out1.txt
Out2.txt

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
# 5  
Old 10-10-2013
create a script test.sh =

cat test.sh contains below commands -
Code:
awk '($1 ~ /ABC_XY_/){print $0}' file1.txt > out1.txt
awk '($1 !~ /ABC_XY_/){print $0}' file2.txt > out2.txt


Last edited by radoulov; 10-10-2013 at 06:11 AM..
# 6  
Old 10-10-2013
Quote:
Originally Posted by kajalvikky
create a script test.sh =

cat test.sh contains below commands -
Code:
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 :-)
# 7  
Old 10-10-2013
Quote:
Originally Posted by pkfox
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

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

Code:
$ 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

Code:
$ 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a pattern in a file and split the line into two lines

Hi All, Greetings everyone !!! I have a file which has many lines, out of which one line is as below. I need to search for pattern "varchar(30) Select" and if exists, then split the line as below. I am trying to achieve this in ksh. Can anyone help me on this. (8 Replies)
Discussion started by: Pradhikshan
8 Replies

2. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

3. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

5. Shell Programming and Scripting

How to search pattern and add that pattern in next line

Hi All, I am new to shell scripting and need help in scripting using CSH. Here is what I am trying to so, 1. Search a specific string e.g. "task" from "task (input1, out1)". 2. Extract the arguements "input1" and "out1" 3. Add them in separate lines below. eg. "int input1" , " integer out1" ... (7 Replies)
Discussion started by: deshiashish
7 Replies

6. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

7. Shell Programming and Scripting

search more than one pattern with perl on same line

Hi friends, I want to search for some hex error codes in some files. After the hex error code is found, the occurences would be counted. Afterwards the found hex errorcode would be cat into a separate file. Here is my code: #!/usr/bin/perl use File::Basename; my $find = $ARGV; my... (2 Replies)
Discussion started by: sdohn
2 Replies

8. Shell Programming and Scripting

Search a pattern in a file with contents in a single line

Hi all I am searching for a pattern in a file . The file content is in a single line.If am doing a grep or sed for the a particular pattern am getting whole file. I want the result in different lines. attaching the file for reference search pattern "/xxxxxx/hhhh/tttttttt/sss/" and... (4 Replies)
Discussion started by: sparks
4 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies
Login or Register to Ask a Question