I'm using the date +'%D_%T' to store Unix system date and time in a shell variable called $_now:
_now="$(date +'%D_%T')"
echo $_nowOutputs:
01/20/12_16:10:42
I'd like to replace / and : with _. I'm aware of the following sed command:
sed 's/\//_/g
> s/:/_/g' <<<"$_now"Outputs:
01_20_12_16_14_09
How do I specify two pattern within the same sed command to replace | and : with _ so that I can get output as 01_20_12_16_10_42?
You can use any one of the following sed substitute find and replace multiple patterns:
sed -e 's/Find/Replace/g' -e 's/Find/Replace/g' <<<"$var" sed -e 's/Find/Replace/g' -e 's/Find/Replace/g' < inputFile > outputFile out=$(sed -e 's/Find/Replace/g' -e 's/Find/Replace/g' <<<"$var")
OR
sed 's/Find/Replace/g;s/Find/Replace/g' <<<"$var" sed -e 's/Find/Replace/g;s/Find/Replace/g' <<<"$var" sed -e 's/Find/Replace/g;s/Find/Replace/g' < inputFile > outputFile out=$(sed -e 's/Find/Replace/g;s/Find/Replace/g' <<<"$var")
Examples: Find And Replace Sed Substitute Using a Singe Command Line
_now="$(sed -e 's/\//_/g;s/:/_/g' <<<$(date +'%D_%T'))" echo $_now
Sample outputs:
01_20_12_16_22_21
Here is another version:
_now=$(sed 's/[\/:]/_/g' <<<$(date +'%D_%T')) echo "$_now"
Sample outputs:
01_20_12_16_24_42
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 3 comments… read them below or add one }
Alternatively, one could tell `date` the desired format, like date +%Y-%m-%d-%H-%M
Just do:
now=`date +”%m_%d_%y_%H_%M_%S”`
echo $now
Man was showing sed not date command…