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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting lines not wanted in a file

Status
Not open for further replies.
Aug 3, 2001
29
US
I am new to the scripting game. I want to be able to read through an entire file and eliminate records not wanted.

Example:

1st record has 9 15 fields the 9th field has customer id
2nd record thru the last record are associated with cust.
last record is id'd in 1st field

then the records repeat each other with different customer id's.

Thanks for any assistance.
Lennyt
 
So what is it you need to delete, exactly ?
Show us a snippet of the file, too. Dickie Bird
db@dickiebird.freeserve.co.uk
 
files look like this

broken into 80 to 150 char records
always beginning with
ISA..........
GS...........
ST...........
AK1..........
AK9..........
GE...........
SE...........
IEA..........

then
it begins again
ISA,GS,ST,AK1,AK9,GE,SE,IEA

Each set of records are Id by the 4th and/or 7th field in ISA.


thanks for any assistance
 
I want to be able to read through an entire file and eliminate records not wanted.[\i]

We still don't know what you want to delete, though, do we ? Dickie Bird
db@dickiebird.freeserve.co.uk
 
Hi lenjturnbull,

If I understand you correctly, you have a file that has 8 repeating lines that collectively form a super-record.

Usually, when we speak of records, we mean an individual line. But when you say you want to delete a record, I think you really mean you want to delete certain of these 8-line super-records based on some criteria.

Is this correct?

If so, what will your criteria be?


Grant.
 
Grant,

that is correct. all the lines(records) from ISA to IEA i want to delete. The 1st field is the line id "ISA" the 7th field is a Customer number". So that many lines with many customers are in this file. I wish to retain only a certain customer's information.

Thanks,

Len
 
Try

awk -v cno=1 '^ISA/{flg=($7=cno)} flg'input-file

Set cno to the customer number you want. CaKiwi
 
Sorry, typo

awk -v cno=1 '^ISA/{flg=($7==cno)} flg'input-file CaKiwi
 
Hi lenjturnbull,

I wrote the following script for you on my break. It hasn't been tested as I have to get back to work. There are a couple of known issues, which I have commented.

You can run this script to specify that you want to exclude a particular entry (pass the word 'delete' as the first command line argument), or to display only that entry (default).

When you run it, it prints out a list of customers, based on $7 in the ISA line. This may need to be fixed if you have more than 24 (a screenful) of customers.


Syntax:
% my.awk [delete] file

Script:
% cat my.awk
#!/usr/bin/awk -f
# USAGE: my.awk [delete] file

BEGIN{

# Check for option command line argument (delete):
if (ARGV[ARGC-2]=="delete")
{
mode="delete";
delete ARGV[ARGC-2];
}

while ( ( getline < ARGV[ARGC-1] ) > 0 )
{
if ($0) ~ /^ISA/)
{
print $7; # To fix: May scroll off screen.
custarr[$7]=1;
}
}
close (ARGV[ARGC-1]);

do
{
printf(&quot;Select one: &quot;);
getline customer < &quot;-&quot;; # Syntax does not work everywhere.
if ( ! customer in custarr )
{
print &quot;Customer &quot; customer &quot; does not exist.&quot;;
print &quot;Please try again...&quot;;
}

}while ( customer in custarr )
}

/^ISA/ {
do
{
if ( ($7 == customer) && (mode!=&quot;delete&quot;) )
{
print $0;
}
else if ( ($7 != customer) && (mode==&quot;delete) )
{
print $0;
}

if $0 ~ /^IEA/ { break; }

}while ( ( getline ) > 0 )
}


Hope this helps,
Grant.
 
Sorry, another typo

awk -v cno=1 '/^ISA/{flg=($7==cno)} flg'input-file CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top