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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
date manipulation pstanand Shell Programming and Scripting 6 04-03-2008 11:11 AM
Manipulation of Date in Shell jnanesh.b Shell Programming and Scripting 1 01-11-2008 01:16 PM
Help with log manipulation StevePace Shell Programming and Scripting 3 01-31-2006 08:28 PM
date manipulation jalburger Shell Programming and Scripting 3 05-10-2004 02:09 PM
Time/date manipulation choice High Level Programming 1 02-01-2002 04:06 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 04-02-2007
mgirinath mgirinath is offline
Registered User
  
 

Join Date: May 2005
Posts: 69
Date Manipulation

I need to achieve the following.....I seached the forum but could not find it...

This is I have in a file...
HTML Code:
"CH","TIA","10/27/2006",000590
I need the date in the third field to be attached to fileas 20061027_test.txt

How do I do it.
  #2 (permalink)  
Old 04-02-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
$ cat file
"CH","TIA","10/27/2006",000590
$ sed 's;.*"\([0-9]*\)/\([0-9]*\)/\([0-9]*\).*;\3\1\2;' file
20061027
Code:
dt=$( sed 's;.*"\([0-9]*\)/\([0-9]*\)/\([0-9]*\).*;\3\1\2;' file )
mv filenm ${dt}"_"filename
  #3 (permalink)  
Old 04-02-2007
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,887
Quote:
Originally Posted by mgirinath
[...]This is I have in a file...
HTML Code:
"CH","TIA","10/27/2006",000590
I need the date in the third field to be attached to fileas 20061027_test.txt
[...]
Code:
awk 'FNR==1{gsub(/\"/,"",$3);split($3,dt,"/")}
{print>(dt[3] dt[2] dt[1] "_" FILENAME)}' FS="," inputfile[s]

Use nawk on Solaris.
  #4 (permalink)  
Old 04-02-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,540
if you have Python, here's an alternative
Code:
import os
myfile = "filename"
for line in open("file"):
    dt = line.split(",")[2][1:-1].split("/")
    newname = "%s%s%s_test.txt" %(dt[2],dt[0],dt[1])
    os.rename(myfile,newname) #rename file to new filename with date attachment
  #5 (permalink)  
Old 04-03-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 603
Code:
dt_str=$(awk -F"," '{gsub(/"/,"",$3); split($3,arr,"/"); print arr[3] arr[1] arr[2];}'  filename)
mv filename ${dt_str}"_"filename
  #6 (permalink)  
Old 04-03-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,957
Code:
perl -e 'while(<>) { chomp; s/\"//g; @a = split(/,/); @now = split(/\//, $a[2]); system("mv filename filename_$now[2]$now[0]$now[1]"); }' filename
  #7 (permalink)  
Old 04-03-2007
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,887
With gawk:

Code:
awk 'FNR==1{gsub(/\"/,"",$3);split($3,dt,"/")}
{print "mv", FILENAME, (dt[3] dt[2] dt[1] "_" FILENAME)|"sh";nextfile}' FS="," inputfile[s]
Otherwise:

Code:
awk 'FNR==1{gsub(/\"/,"",$3);split($3,dt,"/")}
{f[FILENAME]=(dt[3] dt[2] dt[1] "_" FILENAME)}END{for(i in f)print "mv",i,f[i]|"sh"}' FS="," inputfile[s]
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 10:08 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