The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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-11-2008 02:45 AM
Remove duplicates from File from specific location gopikgunda Shell Programming and Scripting 1 04-09-2008 02:16 AM
remove specific lines from flat file using perl meghana Shell Programming and Scripting 12 02-12-2008 09:50 PM
how to remove specific lines from a file bluemoon1 Shell Programming and Scripting 17 10-07-2007 10:40 PM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-23-2005 12:04 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2004
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
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
  #2 (permalink)  
Old 10-04-2004
google's Avatar
google google is offline Forum Advisor  
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?
  #3 (permalink)  
Old 10-04-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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 07:52 AM..
  #4 (permalink)  
Old 10-04-2004
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
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
  #5 (permalink)  
Old 10-04-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #6 (permalink)  
Old 10-04-2004
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
thanks, that works great
  #7 (permalink)  
Old 10-05-2004
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
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 ?
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0