The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing asterix (*) from flat files ctrl_alt_del Shell Programming and Scripting 3 10-18-2007 09:29 AM
Copy all the files with time stamp and remove header,trailer from file ksrams UNIX for Dummies Questions & Answers 35 07-30-2007 02:15 PM
Help with a Flat File!!! kumarsaravana_s Shell Programming and Scripting 11 06-07-2007 11:15 PM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 12:15 PM
Remove header(first line) and trailer(last line) in ANY given file madhunk Shell Programming and Scripting 2 03-13-2006 03:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-23-2007
kumarsaravana_s kumarsaravana_s is offline
Registered User
  
 

Join Date: Feb 2007
Location: Bangalore
Posts: 105
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 (permalink)  
Old 06-23-2007
kamitsin's Avatar
kamitsin kamitsin is offline
Registered User
  
 

Join Date: Nov 2006
Location: /dev/null
Posts: 177
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 !!!!
  #3 (permalink)  
Old 06-23-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
Quote:
Originally Posted by kamitsin View Post
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 (permalink)  
Old 06-23-2007
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Cool

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 (permalink)  
Old 06-23-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
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 View Post
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 (permalink)  
Old 06-23-2007
kumarsaravana_s kumarsaravana_s is offline
Registered User
  
 

Join Date: Feb 2007
Location: Bangalore
Posts: 105
Quote:
Originally Posted by reborg View Post
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 (permalink)  
Old 06-23-2007
kumarsaravana_s kumarsaravana_s is offline
Registered User
  
 

Join Date: Feb 2007
Location: Bangalore
Posts: 105
Quote:
Originally Posted by reborg View Post
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0