Skip to main content

Linux sed - stream editor command

SED stands for stream editor.

To remove last line of any file:

sed -i "$ d" <file_name.txt>

-i means, edit in place --> if you do not give -i then it will not change the result in the main file. Just give you the result on the shell. Giving -i will modify the original file directly at the same time.  



Delete any word: Both single quote and double quote will work.

sed -i 's/unix//g' <file_name.txt>

sed -i 's/\<unix\>//g' <file_name.txt>

sed -i 's/\bunix\b//g' <file_name.txt> 

we want to delete all unix word from the mentioned file. 


Substitute word:

sed -i 's/unix/linux/g' <filename.txt>

we want to substitute all the unix word with linux. 

s for substitute and g for global. If you do not give g, then it will only replace once. for example, say we have unix word in 4 lines. if we do not give g then only first line unix word will be replaced. 2,3, and 4th line will remain same.

gi for ignore character case.


Begins with:

sed -i 's/^unix/linux/g' <filename.txt>

we want to substitute all words with linux that begins with unix.


Ends with:

sed -i 's/os.$/system/g' <filename.txt>

we want to substitute all words with system that ends with os.

 

Exact match:

sed -i 's/\bopen\b/close/g' 

\b for boundary

or 

sed -i 's/\<open\>/close/g'

we want to find exact keyword open and replace with close. 


Replace with nothing in the replacement pattern:

sed -i 's/c//g' <file_name.txt>

find all c and remove all c

all the c's will be removed from the file. 

This is how we can also remove comment # (pound character)

sed -i 's/#.*//g' <filename.txt>

# This is my comment line.

The above command will remove # This is my comment line. on that line because of . (dot) and * 

. (dot) means all characters after hash # and * means whatever the last thing you just typed, say any number of those.

so it will remove # sign and everything that comes after that line which is the whole line (# This is my comment line.)


Remove all spaces or white spaces:

sed -i 's/\s*//g' <filename.txt>    (remove all the spaces)

sed -i 's/\s*#.*//g' <filename.txt>


asdsdfsdfs # i am putting comment here.

it will remove the space before # hash as well as everything after the hash. so remaining data will be asdsdfsdfs

 

We can concatenate two sed commands:

sed -i 's/\s*#.*//g;s/c/C/g' <filename_txt>

It will remove # sign and everything after that line then it will replace lower c with capital C.

 

Removing blank lines:

sed -i '/^$/d' <filename.txt>

or

sed -i '/^[[:space:]]*$/d' <filename.txt>

or

sed -i -r '/^\s*$/d' <filename.txt>


Replacing pattern like example.org to example.com:

say roaster.txt contains the following data:

tom@example.org

billy@example.org

jay@example.com


sed -i -r 's/^(billy|tom)@.*example\.org/\1@example\.com' roaster.txt

The result will be:


tom@example.com

billy@example.com

jay@example.com


Delete all the lines that contains any specific work. for example, 10 lines contains word unix. So the following command will delete all 10 lines. 

sed -i '/unix/d' <filename.txt>


Deleting first word of each line of a file:

sed -i 's/[^ ]* //' <filename.txt>



To replace multiple blank spaces with a single space:

sed -i  's/  */ /g' <filename.txt>


To remove empty lines or those beginning with # from the Apache configuration file, do:

sed '/^#\|^$\| *#/d' httpd.conf

The caret sign followed by the number sign (^#) indicates the beginning of a line, whereas ^$ represents blank lines. The vertical bars indicate boolean operations, whereas the backward slash is used to escape the vertical bars.

In this particular case, the Apache configuration file has lines with #’s not at the beginning of some lines, so *# is used to remove those as well.


Inserting single blank line in files:

sed G myfile.txt



Combining sed with cut command:

ip route show | sed -n '/src/p' | sed -e 's/  */ /g' | cut -d' ' -f9

see the below image for example.

https://www.tecmint.com/wp-content/uploads/2016/07/Combine-sed-with-Other-Commands.png



Deleting lines from a particular file :

To Delete a particular line say n in this example

Syntax:
 sed 'nd' filename.txt
Example:
 sed '5d' filename.txt


To Delete line from range x to y

Syntax:
 sed 'x,yd' filename.txt
Example:
 sed '3,6d' filename.txt

To Delete from nth to last line

Syntax:
 sed 'nth,$d' filename.txt
Example:
 sed '12,$d' filename.txt






Avi



 

Comments

Popular posts from this blog

Install Nessus from docker

Docker installation. Give the below commands one by one. apt install docker-cli or apt install docker.io After the installation is complete, if you are inside wsl then give this command to start docker, because inside wsl systemd (systemctl) does not work: service docker start WSL troubleshooting : If the above command " service docker start " does not work then use below command: dockerd (It may not work if any previous docker process is running. It will show you pid of that process. Use this command to kill that process " kill -9 pid " and run dockerd command again) If " docker ps -a " giving error like " Cannot connect to the Docker daemon at unix:///run/podman/podman.sock. Is the docker daemon running? " This is because you may installed podman-docker package. If you remove the package still you will get this error but you should remove the package. Then issue this command: env | grep -i docker DOCKER_HOST=unix:///run/podman/podman.sock   --...

Installtion of SQLMutant tool

This tool is perfectly works on ubuntu 24 system. And I found it is not working properly in kali linux 24 version.   https://github.com/blackhatethicalhacking/SQLMutant/tree/main This tool need to use along with sqlmap tool. Showing this cheat sheet for kali or debian based system.  This tool actually analyze everything and give you the vulnerable url where sql injection is possible. You just need to use then sqlmap to exploit that.   Prerequisite: apt install pipx -y (for ubuntu) pip3 install uro or pipx install uro pipx ensurepath pipx completions  (not needed)  source ~/.bashrc   or restart system If go tool is not installed then run the below two commands first ( golang-go ) or follow this link to install go (https://mahimfiroj.blogspot.com/2024/12/installing-nuclei-in-kali.html) otherwise skip this step.   dpkg -l | grep packagename (Using this command you can check package is installed or not) apt install gccgo-go -y or apt install gol...

Installing nuclei and go tool in kali

 First you need to install go: https://go.dev/doc/install You need to download this go tool go1.23.4.linux-amd64.tar.gz by clicking the Download button.  Say you are root and download the tool in your Downloads directory. Now run the below command: tar -C /usr/local -xzf go1.23.4.linux-amd64.tar.gz (if this cmd fails then you need to move this tool to /usr/local folder then run this cmd tar -xzf  go1.23.4.linux-amd64.tar.gz) Now add /usr/local/go/bin to the PATH environment variable. You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation): export PATH=$PATH:/usr/local/go/bin Now use the following command for immediate effect. Preventing you from log off then log back in: source $HOME/.profile go version (to check it is installed successfully) Install nuclei: go install -v github.com/projectdiscovery/nuclei/v3/cmd/nuclei@latest or apt install nuclei nuclei -update-templates nuclei -u https://www.domain....