Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stripping lines completely from file into new file

Status
Not open for further replies.

hcclnoodles

IS-IT--Management
Jun 3, 2004
123
0
0
GB
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 either 001 or 002. 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 just the 001'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
 
Here's one way using perl.
Code:
#!/usr/bin/perl -w

$file = shift;

$file1 = "${file}1";
$file2 = "${file}2";

open FILE, $file or die;
open FILE1, ">$file1" or die;
open FILE2, ">$file2" or die;

while (<FILE>) {
    # determine destination
    select substr($_, 44, 3) eq "002" ? *FILE2 : *FILE1;
    print;
}
rename $file1, $file;
Cheers, Neil
 
Try
Code:
egrep ^.{44}001 input_file >> file_with_001
egrep ^.{44}002 input_file >> file_with_002

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
Something like this ?
awk '{status=substr($0,45,3);print>"file"status}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi there I tried using this comand against my file , but the outputfile is empty, I even tried adjusting the {44} to {45} asthis is where the first character of the 3 sits (ie the first 0 in 001), can anybody tell me where im going wrong

egrep ^.{44}001 inputfile >> outputfile
 
sorry to repost but does anybody know why this command wont work, also , I would really like to also look at PHV's awk command, but I dont quite understand the syntax (ie is there no outputfile?) could someone explain to me the syntax of this statement

cheers
 
could someone explain to me the syntax of this statement
awk '{
launch awk and begin the awk program with no pattern, ie for all input records
status=substr($0,45,3);
set the value of variable status to the 45th, 46th and 47th character of the current input line
print>"file"status
write the current line to a file named file001 or file002, depending of the value of the variable status
}' /path/to/input
end the awk program and tell him which input file to proceed

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I'm sorry the egrep solution doesn't work for you. I tested it with a test file which looked like
Code:
abc123def
abc124def
abc123def
abc124def
and used
Code:
egrep ^.{3}123 testfile >> ofile1
egrep ^.{3}124 testfile >> ofile2
I'm using ksh on AIX 5.1 which might make a difference as I believe egrep differs bewteen different flavours of *nix

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
I see, thanks PHV but to simplify my original question I only mentioned 2 status's 001 and 002. There are in fact many of these (ie 004 &005 etc), and if i understand your command, then it will create a new individual file for each of these, I just wish to extract the lines with 002 into a new file and everything else stays together

apologies for not making this clearer
 
002 into a new file and everything else stays together
Something like this ?
awk '
/substr($0,45,3)=="002"{print > "file_with_002";next}
{print > "file_without_002"}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top