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
# 8  
Old 10-10-2013
Code:
awk '{f="out" (/^ABC_XY_/?1:2) ".txt";print $0 > f }' infile

# 9  
Old 10-10-2013
Quote:
Originally Posted by satyaatcgi
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

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

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

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

Quote:
Originally Posted by Akshay Hegde
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


The output is fine but on my system the files created are 1.text and 2.text and I don't know why
# 10  
Old 10-10-2013
Which awk you are using ? which OS ?

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

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

if suppose by mistake if you made f="Out"++i".txt" to f=++i".txt" It will create 1.txt,2.txt.....n.txt
# 11  
Old 10-10-2013
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

Code:
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.
# 12  
Old 10-10-2013
Quote:
Originally Posted by pkfox
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

Code:
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
# 13  
Old 10-10-2013
Quote:
Originally Posted by ctsgnb
Code:
awk '{f="out" (/^ABC_XY_/?1:2) ".txt";print $0 > f }' infile

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

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

Quote:
Originally Posted by Akshay Hegde
I have no Idea about android
As Android is based on Linux/Unix I thought it would be consistent. ( silly me )
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