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

API hacking lab setup

 Follow the commands to install and configure API hacking lab: 1. Install kali linux and update all the packages.  apt update -y apt upgrade -y or apt dist-upgrade -y or apt full-upgrade -y If you face any problem regarding update, install cloud flare warp in the host machine, then again start updating packages in your kali vm.  2. Install and configure burpsuite professional.  Open burpsuite and go to Extender tab. Click on BAppStore. Search for Autorize extension, It will help us to automate authorization testing. Click on Download Jython. From Jython website click on Jython standalone and save it. Go to Extender > Options and under python environment select the jython jar file that you just downloaded. Now again go to BAppStore and re-search for Autorize extension. You will see Install option this time after selecting Autorize extension. Install it. You will see all the installed extensions under Extender > Extensions tab.  3. Install foxy proxy to prox...

Installing Codename SCNR web application scanner on ubuntu | kali

  Perform the following steps from a non-root user. We will go for manual installation.  https://github.com/scnr/installer?tab=readme-ov-file#manual-installation https://github.com/scnr/installer/releases wget https://github.com/scnr/installer/releases/download/v1.7.3/scnr-v1.7.3-linux-x86_64.tar.gz   (Download using normal user) tar -xvzf scnr-v1.7.3-linux-x86_64.tar.gz cd scnr-v1.7.3 cd bin Now go to their website ( https://ecsypno.com/products/scnr ) and subscribe for community edition license from your official email.  ./scnr_activate 6XQ97FW3LVBECD0UJ5H214 ./scnr https://www.example.net/Login.aspx --system-slots-override Now they generate .ser format report after testing the application by default which is hard to read. We need html report. So for example, to generate an HTML report: ./scnr_reporter --report=html:outfile=my_report.html.zip /home/user/.scnr/reports/report.ser Avi

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   --...