The UNIX and Linux Forums  

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
How to search for files based on the time stamp sunny_03 UNIX for Dummies Questions & Answers 1 02-12-2008 09:45 AM
Append Header and Trailer balzzz UNIX for Dummies Questions & Answers 2 01-06-2008 08:19 AM
Need to delete the files based on the time stamp of the file samudha UNIX for Dummies Questions & Answers 2 06-20-2007 08:02 AM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 01: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 07-23-2007
ksrams ksrams is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 17
Copy all the files with time stamp and remove header,trailer from file

All,

I am new to unix and i have the following requirement.

I have file(s) landing into input directory with timestamp, first i want to copy all these files into seperate directory then i want to rename these files without timestamp and also remove header,trailer from that file..

Could anybody please throw some light on this how to write a script for above requirement ..

Thanks in advance..
  #2 (permalink)  
Old 07-23-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Ksrams,
Please give us more information, such as sample of file name with the
timestamp and the expected output.
  #3 (permalink)  
Old 07-23-2007
ksrams ksrams is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 17
Thanks for quick reply Shell_Life.
Quote:
Originally Posted by Shell_Life View Post
Ksrams,
Please give us more information, such as sample of file name with the
timestamp and the expected output.
Here is the info.
Input file(s) located under directory /input and file names are:
1) input1_20070723_171556.txt
2) input2_20070723_171556.txt
3) input3_20070723-171556.txt
input1_20070723_171556.txt sample data:

HEADERRECORD 20070723 DESC
aaa 111 222 333 bbb
bbb 222 111 444 ccc
ccc 333 444 111 ddd
TRAILERRECORD 3


I want the do the following process in one script..
a) First thing is i want to copy all the files into /ouput directory
b) rename the files under /out directory to
1) rename input1_20070723_171557.txt file to input1.txt and also remove HEADERRECORD 20070723 DESC(header record i.e always first line) and TRAILERRECORD 3(Detail record i.e always last line in file) from input1.txt
input1.txt file data should be:

aaa 111 222 333 bbb
bbb 222 111 444 ccc
ccc 333 444 111 ddd


2) rename input2_20070723_171557.txt to input2.txt and also remove first and last lines from input2.txt
3) rename input3_20070723_171556.txt to input3.txt and also remove first and last lines from input3.txt

I hope this is much better compare to my previous post.
I would appreciate if you guide me on this..

Thanks in advance..
  #4 (permalink)  
Old 07-24-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Script

I think this script should help you


Code:
#!/bin/ksh

Input_Dir="$HOME/input"
Output_Dir="$HOME/out"

rm -f $Output_Dir/*
for file in `ls $Input_Dir`  # If the input directory contains only files
# for file in `ls -l $Input_Dir | grep '^-' | awk '{print $9}'` #Only files in Dir
do
        new_file=${file%%_*}
        wc -l $Input_Dir/$file | awk '{ print $1}' | read line_count
        sed "1d;$line_count""d" $Input_Dir/$file > $Output_Dir/$new_file.txt
        touch -r $Input_Dir/$file $Output_Dir/$new_file.txt
done
exit

  #5 (permalink)  
Old 07-24-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695

Code:
mInDir='/Input/'
mOutDir='/Output/'
mFileExp='input*.txt'
for mFile in `find ${mInDir} -type f -name "${mFileExp}"`
do
  mFName=`echo ${mFile} | sed 's/.*\(input.*\)_20.*txt/\1/'`
  mOutFile=${mOutDir}${mFName}'.txt'
  sed '1d;$d' ${mFile} > ${mOutFile}
  rm -f ${mFile}
done

  #6 (permalink)  
Old 07-24-2007
ksrams ksrams is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 17
Quote:
Originally Posted by Shell_Life View Post
Code:
mInDir='/Input/'
mOutDir='/Output/'
mFileExp='input*.txt'
for mFile in `find ${mInDir} -type f -name "${mFileExp}"`
do
  mFName=`echo ${mFile} | sed 's/.*\(input.*\)_20.*txt/\1/'`
  mOutFile=${mOutDir}${mFName}'.txt'
  sed '1d;$d' ${mFile} > ${mOutFile}
  rm -f ${mFile}
done
Hi Shell_Life,

Input files not always starts with input*.txt,input files could start with any name.I want to process all the files under input directory and filename last 20 characters are always timestamp.txt..Could you please modify this script to process all the files instead of input*.txt files.Would it possible if we pass input and output directories as parameters to this script.

Thanks in advance..
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 01:55 AM.


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