NAWK to remove lines that matches a specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NAWK to remove lines that matches a specific pattern
# 1  
Old 12-23-2010
NAWK to remove lines that matches a specific pattern

Hi,

I have requirement that I need to split my input file into two files based on a search pattern "abc"

For eg. my input file has below content

abc
defgh
zyx

I need file 1 with
abc

and file2 with
defgh
zyx


I can use grep command to acheive this. But with grep I need to scan the input file twice to get the desired output. Also with huge files grep just hangs the system.

Can you please help me if I can use NAWK for this requirement?


Thanks
# 2  
Old 12-23-2010
A more suitable input file as an input will make more sense.
# 3  
Old 12-23-2010
# 4  
Old 12-23-2010
Code:
awk '/^abc/{print > "file1"; next}1' inputfile > file2

# 5  
Old 12-23-2010
Hi Frank,

Thanks. It worked. Could you please tell me if I can pass the pattern, name of file1 and file2 as parameters to AWK.



Thanks
# 6  
Old 12-23-2010
If file1 is in variable file1_name and file2 is in variable file2_name
Code:
awk '/^abc/{print > file1; next}1' file1=$file1_name inputfile > $file2_name

# 7  
Old 12-23-2010
Thanks Anurag,

Can I pass pattern also as a parameter. In my case "abc" is pattern that has to be searched
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

4. Shell Programming and Scripting

remove contents including the tag if pattern matches

Hi all, Can anyone help me on this. I have several WP sites that are affected by sql injections. But the contents are different as follows western union india belgaum western union india bolegaon western union india barhaj western union india budhana western union india belda western... (6 Replies)
Discussion started by: sanjuabraham
6 Replies

5. Shell Programming and Scripting

awk to sum specific field when pattern matches

Trying to sum field #6 when field #2 matches string as follows: Input data: 2010-09-18-20.24.44.206117 UOWEXEC db2bp DB2XYZ hostname 1 2010-09-18-20.24.44.206117 UOWWAIT db2bp DB2XYZ hostname ... (3 Replies)
Discussion started by: ux4me
3 Replies

6. Shell Programming and Scripting

Merge lines if pattern matches in ksh

I have a file like this. Pls help me to solve this . (I should look for only Message : 111 and need to print the start time to end time Need to ignore other type of messages. Ex: if first message is 111 and second message is 000 or anything else then ignore the 2nd one and print start time of the... (1 Reply)
Discussion started by: mnjx
1 Replies

7. Shell Programming and Scripting

Merge lines from one file if pattern matches

I have one comma separated file (a.txt) with two or more records all matching except for the last column. I would like to merge all matching lines into one and consolidate the last column, separated by ":". Does anyone know of a way to do this easily? I've searched the forum but most talked... (6 Replies)
Discussion started by: giannicello
6 Replies

8. Shell Programming and Scripting

Remove if the above line matches pattern

but keep if does not I have a file: --> my.out foo: bar foo: moo blarg i am on vacation foo: goose foo: lucy foo: moose foo: stucky groover@monkey.org foo: bozo grimace@gonzo.net dear sir - blargo blargo foo: goon foo: sloppy foo: saudi gimme gimme gimme (3 Replies)
Discussion started by: spacegoose
3 Replies

9. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

10. UNIX for Dummies Questions & Answers

How to select lines in unix matches a pattern at a particular position

I have huge file. I want to copy the lines which have first character as 2 or 7, and also which has fist two characters as 90. I need only these records from file. How I can acheive this. Can somebody help me..... (2 Replies)
Discussion started by: cs_banda
2 Replies
Login or Register to Ask a Question