![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Which OS and shell are you using, and can we see a sample set of the data?
|
|
#3
|
||||
|
||||
|
Something like
Code:
grep -v '.\{44\}002.*' infile > outfile
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. |
|
#4
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
thanks, that works great
|
|
#7
|
|||
|
|||
|
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 ? |
|||
| Google The UNIX and Linux Forums |
| Tags |
| linux, regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|