- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to change date format using sed or awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 08:29 PM
тАО07-19-2004 08:29 PM
I've got a text file in the following format:
abcdefg|MM/DD/YY|00:30|xyzxyz
where MM=month, DD=day and YY=year
How can I change the format into:
abcdefg|YY/MM/DD|00:30|xyzxyz
using sed or awk?
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 08:49 PM
тАО07-19-2004 08:49 PM
Re: How to change date format using sed or awk
Try this script procedure:
TEXT=`cat text_file`
FIELD_1=`echo $TEXT|awk -F"|" '{ print $1 }'`
FIELD_2=`echo $TEXT|awk -F"|" '{ print $2 }'`
FIELD_3=`echo $TEXT|awk -F"|" '{ print $3 }'`
FIELD_4=`echo $TEXT|awk -F"|" '{ print $4 }'`
DATE=`echo $FIELD_2|awk -F"/" '{ print $3"/"$1"/"$2 }'`
echo "$FIELD_1|$DATE|$FIELD_3|$FIELD_4" > text_file
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 09:08 PM
тАО07-19-2004 09:08 PM
Re: How to change date format using sed or awk
filein=$1
cat $filein|while read linein
do
OLDIFS=$IFS
export IFS="|"
echo "$linein"|read a b c d
export IFS="/"
echo "$b"|read e f g
export IFS=$OLDIFS
echo "$a""|""$g""/""$e""/""$f""|""$c""|""$d"
done
Steve Steel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 09:19 PM
тАО07-19-2004 09:19 PM
Re: How to change date format using sed or awk
Actually, I need to change five different files and the number of fields varies from file to file. Unfortunately, there are more than 20 fields in some files.
Hence, it would be better if there is a function to handle all these files.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 09:20 PM
тАО07-19-2004 09:20 PM
Re: How to change date format using sed or awk
FLD="abcdefg|MM/DD/YY|00:30|xyzxyz"
echo "$(echo $FLD | awk -F "|" '{ print $1 }')|$(echo $FLD | awk -F "|" '{ print $2 }' | awk -F "/" '{ print $3"/"$2"/"$1 }')|$(echo $FLD | awk -F "|" '{ print $3"|"$4 }')"
It will give you the desired output.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 09:21 PM
тАО07-19-2004 09:21 PM
Re: How to change date format using sed or awk
perl -pne 's#(..)/(..)/(..)#$3/$1/$2#' textfile > newfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 09:31 PM
тАО07-19-2004 09:31 PM
Re: How to change date format using sed or awk
fun1()
{
filename=$1
newfile=/tmp/test.log
rm -f $newfile
touch $newfile
while read line; do
echo line | grep -q "[[:alpha:]]*/[[:alpha:]]*/[[:alpha:]]"
if [[ $? -eq 0 ]]; then
operation of awk to change the format >> newfile
else
echo $file >>newfile
done < filename
mv $newfile $filename
}
fun1
...
fun1
It is good to have your file examples to make a scipt appropriate to ur requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 10:25 PM
тАО07-19-2004 10:25 PM
Re: How to change date format using sed or awk
$>cat file
abcdefg|MM/DD/YY|00:30|xyzxyz
jdfds|07/20/04|HH:MM|jsfklm
fdslmkqjfq|06/15/03|23:14|jfds
$>for i in `cat file`
> do
> echo "`echo $i | cut -d '|' -f 1`|\c"
> echo "`echo $i | cut -d '|' -f 2 | cut -d '/' -f 2`/\c"
> echo "`echo $i | cut -d '|' -f 2 | cut -d '/' -f 1`/\c"
> echo "`echo $i | cut -d '|' -f 2 | cut -d '/' -f 3`|\c"
> echo "`echo $i | cut -d '|' -f 3,4`"
> done
abcdefg|DD/MM/YY|00:30|xyzxyz
jdfds|20/07/04|HH:MM|jsfklm
fdslmkqjfq|15/06/03|23:14|jfds
Regards,
Fred
"Reality is just a point of view." (P. K. D.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2004 02:21 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2004 02:29 AM
тАО07-20-2004 02:29 AM
Re: How to change date format using sed or awk
sed 's!|\(..\)/\(..\)/\(..\)|!|\3/\1/\2|!' yourFile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2004 02:32 AM
тАО07-20-2004 02:32 AM
Re: How to change date format using sed or awk
echo "hello|10/17/03|abc" | awk -F\| 'function swap(x) { split(x,a,"/"); return a[3] "/" a[2] "/" a[1] };{print swap($2)}'
You can call "swap" multiple times within your awk script. My sample is a one-line but you may want to put the awk script into a seperate file.
HTH
-- Rod Hills