The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove the specific lines from file using perl dipakg Shell Programming and Scripting 4 06-10-2008 11:45 PM
Remove duplicates from File from specific location gopikgunda Shell Programming and Scripting 1 04-08-2008 11:16 PM
remove specific lines from flat file using perl meghana Shell Programming and Scripting 12 02-12-2008 06:50 PM
how to remove specific lines from a file bluemoon1 Shell Programming and Scripting 17 10-07-2007 07:40 PM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-22-2005 09:04 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 10-04-2004
Registered User
 

Join Date: Mar 2002
Posts: 194
remove specific lines from a file

Hi there

I have a file with a variable amount of rows but the 45th, 46th and 47th charachter of each line is the status field which is a three digit code ie 001, 002, 003 etc. My question is this..I need to strip all the records/lines with 002's out of the file completely and put them into another file leaving the original file with everything but the 002's. Sorry for the Newbieness of this question but im a bit stuck on this one

any help on this would be greatly appreciated
Cheers
Gary
Reply With Quote
Forum Sponsor
  #2  
Old 10-04-2004
google's Avatar
Moderator
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Which OS and shell are you using, and can we see a sample set of the data?
Reply With Quote
  #3  
Old 10-04-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Something like
Code:
grep -v '.\{44\}002.*' infile > outfile
Should have the desired effect.

Cheers
ZB

EDIT:
A safer bet would be including "^" in the expression, i.e.
grep -v '^.\{44\}002.*' infile > outfile

Last edited by zazzybob; 10-04-2004 at 04:52 AM.
Reply With Quote
  #4  
Old 10-04-2004
Registered User
 

Join Date: Mar 2002
Posts: 194
example file

xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxx,001,xxx
xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxx,002,xxx
xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxx,004,xxx
xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxx,007,xxx
xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxx,002,xxx
xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxx,002,xxx

os = solaris9
shell = ksh

I tried the command

grep -v '.\{44\}002.*' infile > outfile

but this just removes the 002's completely and populates the outfile with whatever is left, how can I can i create a second outfile (say outfile2) using the same method but this time putting just the 002's in there, as i say, i need tio keep the extracted 002's in a seperate file


cheers
Reply With Quote
  #5  
Old 10-04-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
To have just the 002's don't use the -v option to grep.

i.e.

grep '.\{44\}002.*' infile > outfile2

So we've got

grep '.\{44\}002.*' infile > 002s_only
grep -v '.\{44\}002.*' infile > everything_else

I'd suggest you have a read through the grep manual page, also "man 7 regex" on Linux, "man 5 regexp" on HP-UX (not too sure about Solaris) will give info on Regular Expressions. (else there's always google!).

Study regular expressions. If you intend to work at the Unix command line, they will prove invaluable.

Cheers
ZB
Reply With Quote
  #6  
Old 10-04-2004
Registered User
 

Join Date: Mar 2002
Posts: 194
thanks, that works great
Reply With Quote
  #7  
Old 10-05-2004
Registered User
 

Join Date: Mar 2002
Posts: 194
Hi again, ok I thought this was resolved but they now want to extract the 002's and the 003's into a file and the rest into another file, so basically I need to add some sort of AND operator into this command


grep '.\{44\}002.*' infile > outfile

so i sort of want it to do

grep '.\{44\}002 AND 003.*' infile > outfile


obviously this means that when I use the -v to extract all non 002/003 lines i will need to use this AND operator aswell


is this possible ?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0