Deletion of records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deletion of records
# 1  
Old 07-31-2009
Deletion of records

Hi,

I have file with footer (5records) at end of the file. I want to delete the footer as well as the empty records between the main records and footer record.(The gap between main records and Footer records is dynamic)

Example:


Code:
 
MainRecord1  "115494",","FAELD","CT","
MainRecord2  "245774"," ,"","Gp"
MainRecord3  "165295","Aive","AHS","S",""
MainRecord4  "256254"," MOTOR "

"Footer1 Export AHIS "
"Footer2 Copyright (c) 2000-2009 "
"Footer3 - Do Not Distribute"
"Footer4  7/29/2009 10:24 AM"
"Footer5 Services"

Output File :

Code:
 
MainRecord1"115494",","FAELD","CT","
MainRecord2"245774"," ,"","Gp"
MainRecord3"165295","Aive","AHS","S",""
MainRecord4"256254"," MOTOR "

Can you plase help me for this requirement.
# 2  
Old 07-31-2009
Can you post real world examples. If that all are the correct string i'll just
Code:
-bash-3.2$ grep MainRecord file
MainRecord1  "115494",","FAELD","CT","
MainRecord2  "245774"," ,"","Gp"
MainRecord3  "165295","Aive","AHS","S",""
MainRecord4  "256254"," MOTOR "
-bash-3.2$

# 3  
Old 07-31-2009
This awk script removes all the empty lines and footer lines :
Code:
awk 'NF && $0 !~ /"Footer/' inputfile

Inputfile:
Code:
MainRecord1  "115494",","FAELD","CT","
MainRecord2  "245774"," ,"","Gp"
MainRecord3  "165295","Aive","AHS","S",""
MainRecord4  "256254"," MOTOR "

MainRecord5  "987654","Xyz "


"Footer1 Export AHIS "
"Footer2 Copyright (c) 2000-2009 "
"Footer3 - Do Not Distribute"
"Footer4  7/29/2009 10:24 AM"
"Footer5 Services"

Output:
Code:
MainRecord1  "115494",","FAELD","CT","
MainRecord2  "245774"," ,"","Gp"
MainRecord3  "165295","Aive","AHS","S",""
MainRecord4  "256254"," MOTOR "
MainRecord5  "987654","Xyz "

If you want to keep empty lines between Main records :
Code:
awk '
/"Footer/ { exit }
NF        { print emptylines $0 ; emptylines = "" ; next }
          { emptylines = emptylines "\n" }
' inputfile

Output:
Code:
MainRecord1  "115494",","FAELD","CT","
MainRecord2  "245774"," ,"","Gp"
MainRecord3  "165295","Aive","AHS","S",""
MainRecord4  "256254"," MOTOR "

MainRecord5  "987654","Xyz "

Jean-Pierre.
# 4  
Old 07-31-2009
In my actual file, there wouldn't be any name called "MainRecord" or "Footer" ...for understanding purpose I have given previously...Here you can fine the replica of the actual file....

Code:
"115494","G","Gap"
"245774","","AHIS","Gap"
"165295","MCET, INC.","SUN CHERAY","PA","15S","Gap"
"256254","WINENARDTOWN","MD","20IS","Gap"


"Exlments"
"Copyts reserved."
"Conf Distribute"
"Gene09 10:24 AM"
"Dealvices"


Outputfile should look like this...

Code:
"115494","G","Gap"
"245774","","AHIS","Gap"
"165295","MCET, INC.","SUN CHERAY","PA","15S","Gap"
"256254","WINENARDTOWN","MD","20IS","Gap"

# 5  
Old 07-31-2009
if you're actual record has " and a number..
Code:
-bash-3.2$ grep ^\"[1-9] file
"115494","G","Gap"
"245774","","AHIS","Gap"
"165295","MCET, INC.","SUN CHERAY","PA","15S","Gap"
"256254","WINENARDTOWN","MD","20IS","Gap"
-bash-3.2$

you can also try Jean-Pierre code
# 6  
Old 07-31-2009
ryandegreat25..

I am not sure wether each record will start with " and a number....

But, I am sure , after the main records are over, there will be an empty record/s....form there onwards, what ever the record/s will come is/are as a footer... So we need to delete all the records after the empty record.

Can you please help ....
# 7  
Old 07-31-2009
This code removes all lines starting from the first empty line :
Code:
awk '!NF { exit } 1' inputfile

jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

2. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

3. Shell Programming and Scripting

Split records into multiple records

Hi All, I am trying to split a record into multiple records based on a value. Input.txt "A",1,0,10 "B",2,0,10,15,20 "C",3,11,14,16,19,21,23 "D",1,0,5 My desired output is: "A",1,0,10 "B",2,0,10 "B",2,15,20 "C",3,11,14 "C",3,16,19 "C",3,21,23 (4 Replies)
Discussion started by: kmsekhar
4 Replies

4. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

5. Shell Programming and Scripting

advanced deletion

How can i delete the contents of the directory except one file? (8 Replies)
Discussion started by: proactiveaditya
8 Replies

6. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

7. Forum Support Area for Unregistered Users & Account Problems

deletion ofsunburntYux

Could you please close SunburntYux as I would like to use the emaill address on this account to register FloridaBSD, (0 Replies)
Discussion started by: SunBurntYux
0 Replies

8. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies

9. Shell Programming and Scripting

Regarding deletion of old files

Hi, I have a list of directories which contain old files that are to be deleted. I put the list of all directories in a txt file and it is being read by a script Iam searching for the files older than 60 days using mtime and then deleting it But all the files are getting deleted... (3 Replies)
Discussion started by: Chidvilas
3 Replies

10. Post Here to Contact Site Administrators and Moderators

Account Deletion...

Please delete my account? Thanks, so very much, really. (2 Replies)
Discussion started by: spaceshiporion
2 Replies
Login or Register to Ask a Question