LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2010, 06:58 AM   #1
visitnag
Member
 
Registered: Mar 2008
Posts: 147

Rep: Reputation: 15
date substitution problem


My infile:

despatching date 00/00/0000
despatching date 00/00/0000
despatching date 00/00/0000
despatching date 00/00/0000

==========================================================
My code:

clear
echo -n " Enter Date as ddmmyyyy : "
read dat
echo $dat > xx
dd=`cut -c1-2 xx`
mm=`cut -c3-4 xx`
yy=`cut -c5-8 xx`
sed 's/00\/00\/0000/'$dd'\/'$mm'\/'$yy'/g' infile > outfile
rm xx

===========================================================
My Outfile:

despatching date 01/01/2010
despatching date 01/01/2010
despatching date 01/01/2010
despatching date 01/01/2010

Is there any shortcut for this especially in awk? Kindly guide me. Thank you.
 
Old 01-30-2010, 07:07 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Code:
#!/bin/bash

read -p " Enter Date as dd mm yyyy : " DAY MONTH YEAR
sed 's/00\/00\/0000/'$DAY'\/'$MONTH'\/'$YEAR'/g' infile > outfile
seems to work ok

cheers
 
Old 01-30-2010, 09:12 AM   #3
visitnag
Member
 
Registered: Mar 2008
Posts: 147

Original Poster
Rep: Reputation: 15
Yeah! It will work, but the user will get confuse when giving the sapce between date,month,year. I mean they generally give date as 12122010 instead of 12 12 2010. Now gimmi some idea how to specify the fixed place to the date fields i.e.,
as soon as the user enters DAY number it should jump into the MONTH field and then next to YEAR field. Screen display as following...


Enter date as dd mm yyyy : <DAY> / <MONTH> / <YEAR>


I did something different using awk, but it is also almost same like your solution....

clear
read -p " Enter Date as ddmmyyyy : " dat
awk '{
dd=substr('$dat',1,2);mm=substr('$dat',3,2);yy=substr('$dat',5,4);
sub(/00\/00\/0000/,dd"/"mm"/"yy,$0)
print $0
}' infile > outfile

but my request is there should be only
2 digit place for day
2 digit place for month
4 digit place for year

Last edited by visitnag; 01-30-2010 at 09:17 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
sed whitespace substitution problem. arashi256 Linux - Newbie 18 07-17-2009 11:03 AM
adobe reader font substitution problem in Fedora 8 martin78 Linux - Software 0 11-13-2008 03:21 PM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
what is the correct syntax order for tar with --after-date DATE, --newer DAT farhan Linux - General 1 03-16-2007 08:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration