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 > 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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding new line at the end of file somesh_p Shell Programming and Scripting 5 11-05-2007 05:47 PM
How to add filename to the end of each line in file tmikahan Shell Programming and Scripting 2 05-08-2007 05:24 AM
adding a line to a file andy2000 Shell Programming and Scripting 2 03-26-2007 08:44 AM
Adding filename into file content missutoomuch Shell Programming and Scripting 1 11-11-2005 04:02 PM
How to adding the filename into file contents missutoomuch Shell Programming and Scripting 2 11-11-2005 05:26 AM

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

Join Date: Apr 2004
Posts: 34
Grabing Date from filename and adding to the end of each line in the file.

Hi,

I have 24 .dat files something like below. The file name starts with “abc” followed by two digit month and two digit year. Is there a way to grab the month and year from each filename and append it to the end of each line. Once this is done I want to combine all the files into file Master.dat file.
All files are fixed width

abc0103.dat
abc0203.dat
……………………..and so on…………….
abc1203.dat
abc0104.dat
……………………….and so on till……..
abc1204.dat

All files are fixed width and the layout is as below

Field1.... HNUM CHARACTER 1 to 10
Field2.... AKEY CHARACTER 11 to 20
Field3.... BNC CHARACTER 21 to 23
Field4.... AMOUNT NUMERIC 24 to 28
Field5.... BUSS CHARACTER 29 to 29

I want to grab the month and year from each filename and append at the end of each line to the respective files. and create a master.dat file.

I will appreciate any help on this.

Thanks
Raj

Last edited by reborg; 05-01-2007 at 07:50 PM.. Reason: please do not shout in the forums --reborg
  #2 (permalink)  
Old 05-01-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by rkumar28
Hi,

I have 24 .dat files something like below. The file name starts with “abc” followed by two digit month and two digit year. Is there a way to grab the month and year from each filename and append it to the end of each line. Once this is done I want to combine all the files into file Master.dat file.
All files are fixed width

abc0103.dat
abc0203.dat
……………………..and so on…………….
abc1203.dat
abc0104.dat
……………………….and so on till……..
abc1204.dat

All files are fixed width and the layout is as below

Field1.... HNUM CHARACTER 1 to 10
Field2.... AKEY CHARACTER 11 to 20
Field3.... BNC CHARACTER 21 to 23
Field4.... AMOUNT NUMERIC 24 to 28
Field5.... BUSS CHARACTER 29 to 29

I want to grab the month and year from each filename and append at the end of each line to the respective files. and create a master.dat file.

This assumes that the only numbers in the filename are the [malformed] date that you want to extract. If that is not the case, change the gsub line to suit the criteria required to extract the date.

Code:
awk '{
  date = FILENAME
  gsub( /[^0-9]/,"",date )
  print $0, date > "master.dat"
}' *.dat
Sponsored Links
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:51 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