≡ Menu

sed command

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?
[click to continue…]

Linux / Unix: Sed Delete Matching Words In A File

I have a file called rsnapshot.conf. It has the words “ssh_args=-p 1222”. How do I match and remove (delete) the words “ssh_args=-p 1222” from config file using sed command under Linux or Unix like operating systems?
[click to continue…]

Bash Shell: Trim Leading White Space From Input Variables

My script depends upon $1 to take certain actions:

echo “|${file}|”

However, sometime there will leading while space which will result into:

|     output.txt|

How do I trim leading white space from one of my input variables?
[click to continue…]

sed Delete / Remove ^M Carriage Return (Line Feed / CRLF) on Linux or Unix

How can I remove the ^M or ^M (carriage Return / line feed ) from text file using sed under UNIX or Linux operating systems?
[click to continue…]

Linux / UNIX: Sed Replace Newline

How do I replace newline (\n) with sed under UNIX / Linux operating systems?
[click to continue…]

BASH: Prepend A Text / Lines To a File

I am a new Unix system user with a bash as a default shell. I can append text to a file using >> operator but how do I prepend a text to a file? I want the opposite operation of >>. How can I prepend some data to a text file?
[click to continue…]

Linux / UNIX View Only Configuration File Directives ( Uncommented Lines of a Config File )

Most Linux and UNIX-like system configuration files are documented using comments, but some time I just need to see line of configuration text in a config file. How can I view just the uncommented configuration file directives from squid.conf or httpd.conf file? How can I strip out comments and blank lines on a Linux or Unix-like systems?
[click to continue…]

Sed Find and Display Text Between Two Strings or Words

Q. How do I find the text between the strings FOO and BAR inclusive using sed command line option?
[click to continue…]

sed Case Insensitive Search Matching

Q. How do I perform a case-insensitive search using sed under UNIX / Linux? I’d like to match all combination of word – foo, FOO, FoO and so on while replacing or performing other operations.
[click to continue…]

sed Insert a Newline Into the RHS of a Substitution

Q. How do I insert a newline into the RHS of a substitution using sed under Linux / UNIX like operating systems? My line pattern is as follows:

This is a test. This is another input.

I’d like to insert a newline after dot (.).
[click to continue…]