Outputting discarded GREP lines to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Outputting discarded GREP lines to a file
# 1  
Old 04-09-2010
Outputting discarded GREP lines to a file

I had a question about grep.
If I grep for something in a file, the output shows me all the lines in which that 'something' is contained in.
Is there a way to also output all the lines in which that 'something' wasnt contained in.
Say I have a file with a bunch of names and I do:

grep scott names.txt > scottnames.txt

I would like for two files to get created. One in which shows all the lines in which scott is contained, and two, all the lines in which scott isnt contained in.

I know that I can do a grep -v scott names.txt, but is there a way to do both at the same time?

Thanks
# 2  
Old 04-09-2010
Grep dont have that option. But you can do it in awk

Code:
awk ' /scott/{ print > "scottnames.txt" } !/scott/{ print > "othernames.txt" }  'names.txt

# 3  
Old 04-09-2010
If you must do it with grep, then you'll have to read the file twice:
Code:
grep scott names.txt > scottnames.txt
grep -v scott names.txt > othernames.txt

awk (as anbu23 demonstrated) and sed can do it with one pass.

Using (portable) sed:
Code:
sed '/scott/!{w othernames.txt
d;}' names.txt > scottnames.txt

I'm sure a perl solution will soon follow Smilie

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

grep two lines from a file

Sample File abc xyz def abc ggh abc xyz I just created a sample file above to show what I need. I need to grep two lines. e.g abc and xyz(only if they are one after the other) so output would be abc xyz abc xyz (note abc followed by ggh line would not come out in the output). I... (9 Replies)
Discussion started by: ran123
9 Replies

2. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

3. Shell Programming and Scripting

Finding several patterns and outputting 4 lines after

I an trying to parse a file looking for pattern1, or pattern2, or pattern3 and when found print that line and the next 4 lines after it. I am using korn shell script on AIX and grep -A isn't available. (1 Reply)
Discussion started by: daveisme
1 Replies

4. Shell Programming and Scripting

grep two lines in a file

Hi Everyone, I have 1.txt 1 6-6 3-3 word y f 6-6 word 5-5 4 5-5 word The output should be: 3-3 (8 Replies)
Discussion started by: jimmy_y
8 Replies

5. Shell Programming and Scripting

Outputting data from multiple lines

Hi guys, looking for a bit of advise, and as I am a complete novice, please excuse the daft questions!! I have a list of events and of which entry looks like this; # # Event 1 # NAME = Event 1 # 12345 : 123 : 1 : 1 : L,1,N : 1,0 : Event # # Event 2 # NAME = Event 2 # 12346... (8 Replies)
Discussion started by: JayC89
8 Replies

6. Shell Programming and Scripting

outputting data in table from grep results

Hi all. I'm a real unix newbie and looking for some help on a shell scripting problem. I'm going the longest ways around everything but I'm getting there. My next problem however has me stumped. I have set up a program that takes inputs from a user for a particular month and year (and stores them... (2 Replies)
Discussion started by: Chillspark
2 Replies

7. Shell Programming and Scripting

grep: outputting search strings

Newbie question -- any help very much appreciated: I want to be able to get grep (or whatever else would work) to return not only matching lines, but also the original input string: An example may help: Suppose I have two files data1.txt and data2.txt: data1.txt Hello my name is foo. What... (3 Replies)
Discussion started by: tapmas
3 Replies

8. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies

9. Shell Programming and Scripting

Problem with outputting multiple lines

Dear Gurus, I have this output file: F1BDEV13 NTIAF101 2006/09/21 14:54:51 14:55:29 1 0560-0570 LAN F1BDEV14 NTIAF101 2006/09/21 14:55:30 14:55:49 1 0000-0000 LAN F1FSP001 NTIAF101 2006/09/21 14:55:51 14:55:53 1 0000-0000 LAN F1NSP001 ... (6 Replies)
Discussion started by: lweegp
6 Replies

10. Shell Programming and Scripting

PHP Outputting finite amount of lines from a file

Hi, I have the code below which outputs all the lines of a CSV file but I'd like it to only output 15 lines then save the current line as a variable which could be used (as a link) to display the next 15 lines. I can't get my head around the logic! Please help :D while ($data = fgetcsv($fp,... (1 Reply)
Discussion started by: pondlife
1 Replies
Login or Register to Ask a Question