![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Time Between Dates | Sreejith_VK | HP-UX | 2 | 02-26-2008 11:02 PM |
| Comparing two dates | guptan | Shell Programming and Scripting | 5 | 11-25-2005 08:46 AM |
| Searching between dates | Khoomfire | Shell Programming and Scripting | 9 | 08-22-2005 08:11 AM |
| compare two dates | wchen | Shell Programming and Scripting | 6 | 10-24-2002 01:39 PM |
| Formatting dates in a file | maverick | UNIX for Dummies Questions & Answers | 6 | 12-18-2001 08:46 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How do I insert a string into a particular character position using SED,AWK or KSH?
I have a text file with contents (dates) below and I want to change the format from YYYYMMDD to YYYY/MM/DD. Code:
$ more input_date 20060227 20051201 20040130 Code:
$ more format_date.ksh #!/bin/ksh while read line do echo $line > tmp$$ YEAR=$(sed 's/....$//g' tmp$$) DAY=$(sed 's/^......//g' tmp$$) MONTH=$(sed -e 's/^....//g' -e 's/..$//g' tmp$$) rm tmp$$ echo "$YEAR/$MONTH/$DAY" done < $1 Code:
$ format_date.ksh input_date 2006/02/27 2005/12/01 2004/01/30 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
try using the cut command with -c option
|
|
#3
|
|||
|
|||
|
Code:
sed 's/\(....\)\(..\)\(..\)/\1\/\2\/\3/' filename |
|
#4
|
||||
|
||||
|
Using shell builtin's
Code:
#!/bin/ksh
while read line
do
YEAR=${line%????}
day_mon=${line#$YEAR}
MON=${day_mon%??}
DAY=${day_mon#??}
echo "$YEAR/$MON/$DAY"
done < input
|
|
#5
|
|||
|
|||
|
Thanks guys!
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|