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
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 10 08-07-2008 06:54 AM
Adding Multiple Lines to Multiple Files dayinthelife Shell Programming and Scripting 2 06-04-2008 08:50 AM
Ksh Storing Multiple Files and reading each line in each file. developncode UNIX for Dummies Questions & Answers 1 04-08-2008 01:44 PM
Command line tool to join multiple .wmv files? karman OS X (Apple) 2 09-22-2007 10:52 PM
Cat'ing a multiple line file to one line djsal Shell Programming and Scripting 1 04-16-2007 05:50 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-07-2006
Registered User
 

Join Date: May 2006
Posts: 16
How to Eliminate first line of multiple files

hi gurus ,,

I have multiple files with same file pattern..in a particular directory

for ex: file20061101.trf
file20061102.trf
file20061103.trf

Each of the file has a header as column names..

My questions is how can i eliminate the first row of each of these files and write it into a single file..

Can anyone help uup with a shell script..

i was thinking of looping each file and get the count and then do tail of -1 to a file..


but was not sure whether its the best method..if anyone had come across the situation please let me know

Appreciate any kind of information or help

Thanks n Regards
Sish
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-07-2006
sendai's Avatar
Registered User
 

Join Date: Dec 2006
Location: Brasil
Posts: 18
try this one

Code:
#!/usr/bin/perl
$x=1; #first file who will open will be file20061.trf change if you need
$xmax=9999; #change this as you need is the limite of files
$patternb="file2006"; #begin of filename
$patterne=".trf"; #end of filename
while($x!=$xmax){
$y=0;
@lines=();
$file=$patternb . $x . $patterne;
open(FD,"< $file")|| $y=1;
@lines=<FD>;
close(FD);
@lines=reverse(@lines);   #\
pop(@lines);                  # - Remove First Line
@lines=reverse(@lines);   #/
open(FD,"> $file") if $y!=1;
print FD @lines if $y!=1;
close(FD);
$x++; #and loop 
}

ps: the script need to be at the same directory of the files
Reply With Quote
  #3 (permalink)  
Old 12-07-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
Much easier with pipes than perl:
Code:
# Print a list of files with 'find', feed it into while loop.
# For each filename, open file, read and discard first line,
# print rest of file, while redirecting all output into 'output'
find ./ -iname 'file2006*' |
        while read FILE
        do
                ( read LINE ; cat ) < "${FILE}"
        done > output
Reply With Quote
  #4 (permalink)  
Old 12-07-2006
sendai's Avatar
Registered User
 

Join Date: Dec 2006
Location: Brasil
Posts: 18
omg

hey i need to learn more about sh
Reply With Quote
  #5 (permalink)  
Old 12-07-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,325
Any reason that you can't use good old tail? It has a '+' option that you can use to print lines from that line onward:
Code:
# cat test.sh
#!/bin/ksh
echo $1
echo $?

#tail +2 test.sh
echo $1
echo $?
Run tail +2 on each file through the while loop.
Reply With Quote
  #6 (permalink)  
Old 12-07-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
Or...
Code:
awk 'FNR>1' file*.trf > outfile
Reply With Quote
  #7 (permalink)  
Old 12-07-2006
System Shock's Avatar
Registered User
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 378
On a side note, funny how it is always whose is bigger, yet, when it comes to scripting/coding, it is always whose is smaller
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:23 PM.


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 Global Fact Book

Content Relevant URLs by vBSEO 3.2.0