Removing trailer from a flat file!!!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing trailer from a flat file!!!
# 1  
Old 06-23-2007
Removing trailer from a flat file!!!

Hi,

I get some flat files with trailer which gives the totol records count and i want to remove the trailer from the file.

i used the following command it works fine with a single file.

cat file_name | grep -v 'Total records:' > file1
mv file file_name

But i dont know how to remove the trailer when we have more than 1 file and how to redirect them and move them back to the original file name

The syntax of file is :

<date>.file_name.txt

something like 06222007.aaa_ddd_ccc_ddd.txt
06212007.aaa_ddd_ccc_ddd.txt
06202007.aaa_ddd_ccc_ddd.txt

Regards,
Kumar
# 2  
Old 06-23-2007
Code:
#!/bin/ksh
for i in `ls -lrt *file_name.txt|awk '{print $9}'
do
   grep -v 'Total records:' $i > $i_new
   mv $i_new $i
done

Simple !!!!Smilie
kamitsin
# 3  
Old 06-23-2007
Quote:
Originally Posted by kamitsin
Code:
for i in `ls -lrt *file_name.txt|awk '{print $9}'

Not as simple as it should be.

Code:
for file in *file_name.txt ; do
   grep -v 'Total records:' $file > ${file}_new
   mv ${file}_new $file
done

or even simpler if you have gnu sed
Code:
sed -i '/Total records:/d' *file_name.txt

# 4  
Old 06-23-2007
Tools

Try this

Code:
for filename in `ls -1  *aaa_ddd_ccc_ddd.txt`
do 
sed -e '$d' $filename > /tmp/tmpfile
mv /tmp/tmpfile $filename
done


This assumes your trailer is last line always!
# 5  
Old 06-23-2007
RishiPahuja, if you used the same sed expression as I did in the gnu sed you would not depend on it being the last line.

Quote:
Originally Posted by RishiPahuja
Try this
Code:
for filename in `ls -1  *aaa_ddd_ccc_ddd.txt`

Don't do this. The shell has glob pattern, the ls is not required and will break the code if there are spaces in the filename.

The correct way to do this is:
Code:
for fillename in *aaa_ddd_ccc_ddd.txt

# 6  
Old 06-23-2007
Quote:
Originally Posted by reborg
RishiPahuja, if you used the same sed expression as I did in the gnu sed you would not depend on it being the last line.



Don't do this. The shell has glob pattern, the ls is not required and will break the code if there are spaces in the filename.

The correct way to do this is:
Code:
for fillename in *aaa_ddd_ccc_ddd.txt

Thanks a lot friends.I will try these methods and let you know.Thanks again.
# 7  
Old 06-23-2007
Quote:
Originally Posted by reborg
RishiPahuja, if you used the same sed expression as I did in the gnu sed you would not depend on it being the last line.



Don't do this. The shell has glob pattern, the ls is not required and will break the code if there are spaces in the filename.

The correct way to do this is:
Code:
for fillename in *aaa_ddd_ccc_ddd.txt

Hi,

When i run the following code,it gets into an infinite loop and never comes out.
#!/usr/bin/ksh
cd /space/tmp/file

for filename in *aaa_ddd_ccc_ddd.txt
do
grep -v 'Total records:' > $filename_new
mv $filename_new $filename
done
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Removing Header and Trailer record of a EBCDIC file

I have a EBCDIC multi layout file which has a header record which is 21 bytes, The Detail records are 2427 bytes long and the trailer record is 9 bytes long. Is there a command to remove the header as well as trailer record and read only the detail records while at the same time not altering... (1 Reply)
Discussion started by: abhilashnair
1 Replies

2. Shell Programming and Scripting

Verify the header and trailer in file

please see my requirement, I hope I am clear. (9 Replies)
Discussion started by: mirwasim
9 Replies

3. Shell Programming and Scripting

Script to validate file header and trailer

Hi, I need a script that validates a file header/detail/trailer. File layout is: Header - Rec_Type|File_name|File_Date Detail - Rec_Type|field1|field2|field3... Trailder - Rec_Type|File_name|File_Date|Record_count Sample Data: HDR|customer_data.dat|20120709... (7 Replies)
Discussion started by: ash_sh
7 Replies

4. Shell Programming and Scripting

How to add trailer record at the end of the flat file in the unix ksh shell scripting?

Hi, How to add trailer record at the end of the flat file in the unix ksh shell scripting can you please let me know the procedure Regards Srikanth (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

5. UNIX for Dummies Questions & Answers

Adding header and trailer into a file

Hi, I want to add the below Header to all the files in sequence File1,File2,File3...etc "ABC,<number of chracter in the file>" e,g - If File1 is as below pqrstuvdt abcdefgh then I want to add the above header into it ,So that File1 becomes as below ABC,17 pqrstuvdt abcdefgh ... (9 Replies)
Discussion started by: spari2
9 Replies

6. Shell Programming and Scripting

Removing Header & Trailer from a file

Hi All, I am karthik. I am new to this forum. I have one requirement. I have a file with header and footer. Header may be like HDR0001 or FILE20090110 (Assume it is unknown so far, but i am sure there is a header in the file) likewise file has the trailer too. I just... (7 Replies)
Discussion started by: karthi_gana
7 Replies

7. Shell Programming and Scripting

Removing commas within semicolon in a flat file

i am recieving a flat file ( comma seperated ) with comma in between double quotes in any of the source fields . i need to remove the comma in double quotes and process the file thereafter fields in file ========= col1,col2,col3,col4 input can be any of the followng... (31 Replies)
Discussion started by: r_t_1601
31 Replies

8. Shell Programming and Scripting

Removing commas within semicolon in a flat file

Hi , Im relatively new to unix and have to process a comma serparated flat file . I recieve some of the fields in double quotes and i want to remove it .. INPUT ==== filed1,field2,field3,"fie,ld4" OUTPUT ===== field1,field2,field3,"field4" can anyone tell me how to achieve... (10 Replies)
Discussion started by: r_t_1601
10 Replies

9. Shell Programming and Scripting

I want Trailer to be added into the text file.

Hi folks, I want Trailer to be added into the txt file the format is below. flatfile-> abc.txt count of the file is 500 records. I want the trailer in this format: TRAILER|500 (pipe delimeter). Please suggest the comands ASAP. Rgds Ann (5 Replies)
Discussion started by: Haque123
5 Replies

10. Shell Programming and Scripting

Removing asterix (*) from flat files

Hi all, I have to generate a tab-delimited flat file. Since few days, I have been getting * in random accounts. For example, an account supposed to have the value 123456789123,123 Now this is supposed to be in a 12,3 format. Please note that this being a German field, the comma (,) here... (3 Replies)
Discussion started by: ctrl_alt_del
3 Replies
Login or Register to Ask a Question