Linux / Unix: Sed Substitute Multiple Patterns [ Find & Replace ]

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 $_now


I’d like to replace / and : with _. I’m aware of the following sed command:

sed ‘s/\//_/g
> s/:/_/g’ <<<"$_now"


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")


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:


Here is another version:

_now=$(sed 's/[\/:]/_/g' <<<$(date +'%D_%T'))
echo "$_now"

Sample outputs:


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • sylvainulg Jan 23, 2012 @ 10:10

    Alternatively, one could tell `date` the desired format, like date +%Y-%m-%d-%H-%M

  • LOL LOLSKI Dec 21, 2012 @ 16:11

    Just do:

    now=`date +”%m_%d_%y_%H_%M_%S”`
    echo $now

  • gabriel Apr 1, 2013 @ 15:52

    Man was showing sed not date command…

  • Panna Nov 25, 2014 @ 16:04

    I have a not well experience with awk and sed. if any find the solution?
    I have a two variable like..
    a-fdfgh3 ##here two word in one column and may you can say in row
    $var2=a-45afd4 string1 string2 string3 string4
    a-fdfgh3 string1 string2 string3 string4
    a-hfdj3e string1 string2 string3 string4
    a-jkkl3l string1 string2 string3 string4

    i want to find $var1 data in $var2 and the output should be
    Example: a-45afd4 string1 string2 string3 string4
    a-fdfgh3 string1 string2 string3 string4

    ##rest all column row should not come

  • SANDY Mar 9, 2016 @ 15:50

    What is the output of “date -u +%V`uname`|sha512sum|sed ‘s/\W//g'”?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum