The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed remove last 10 characters of a line start from 3rd line minifish Shell Programming and Scripting 7 03-26-2008 01:42 PM
SED help (remove line::parse again::add line) Malumake Shell Programming and Scripting 6 10-24-2007 02:02 PM
Remove header(first line) and trailer(last line) in ANY given file madhunk Shell Programming and Scripting 2 03-13-2006 12:36 PM
Remove first 15 line bobo UNIX for Dummies Questions & Answers 6 01-30-2006 06:26 PM
To remove new line character shihabvk UNIX for Advanced & Expert Users 6 08-18-2005 12:02 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-04-2008
Registered User
 

Join Date: Oct 2007
Posts: 23
remove the first line of all files

I want to remove the first line of all files in a directory with .dat extension. Can any one help me on this with a small script. I want the file names to remain same .
Reply With Quote
Forum Sponsor
  #2  
Old 01-04-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 383
Code:
ls *dat|xargs -i ksh -c 'sed -e "1d" {}>{}.tmp;mv {}.tmp {}'
Reply With Quote
  #3  
Old 01-04-2008
Registered User
 

Join Date: Oct 2007
Posts: 23
it gives an error

stty: : No such device or address
Reply With Quote
  #4  
Old 01-04-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by dineshr85 View Post
I want to remove the first line of all files in a directory with .dat extension. Can any one help me on this with a small script. I want the file names to remain same .
you can use
Code:
# tail +2 file.dat > newfile
or
Code:
# more +2 file.dat > newfile
to get 2nd line of file onwards. I leave it to you to do the rest. Try.
Reply With Quote
  #5  
Old 01-04-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 383
Just for fun:
Code:
awk 'FNR != 1  { print >>FILENAME".tmp";next}
    { print FILENAME 
       ">"FILENAME".tmp" }' *dat|xargs -i mv "{}.tmp" "{}"
Reply With Quote
  #6  
Old 01-04-2008
Registered User
 

Join Date: Dec 2005
Posts: 74
for files in `ls *.dat` #list of all .dat file
do
cp $files $files.bck #make a backup of the file
sed '1d' -i $files #remove the first line of the file
done

Rakesh UV

Last edited by uvrakesh; 01-04-2008 at 04:52 AM. Reason: the comment were to near making it look more unclear
Reply With Quote
  #7  
Old 01-04-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
just for fun: for very large files, use awk/more instead of sed '1d' method.
Code:
# wc -l file1
8998841 file1
# time sed '1d' file1 > sedtest

real    0m28.329s
user    0m26.930s
sys     0m1.000s
# time sed -n '2,$p' file1 > sedtest

real    0m33.740s
user    0m29.842s
sys     0m1.096s

# time more +2 file1 > moretest

real    0m10.629s
user    0m8.353s
sys     0m0.856s
 # time awk 'NR>1' file1 > awktest

real    0m14.618s
user    0m7.476s
sys     0m1.016s
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0