Tag Archives: sed

A tiny script to replace all occurrences of a string in a file with another string

sed -i ‘s/original/new/g’ filename.txt This basically replaces every occurrence of the “original” string with the “new” string in the file named “filename.txt” e.g. sed -i ‘s/192.168.0.1/192.168.1.1/g’ /etc/fstab Will replace all occurrences of the IP “192.168.0.1” with the IP “192.168.1.1” in … Continue reading

Posted in Linux | Tagged , , , | 1 Comment

Replacing a string with another in a text file using sed

sed -i ‘s/old-test/new-text/g’ filename This replaces the word old-test with the word new-text in the file named filename.

Posted in Uncategorized | Tagged , , , | Leave a comment

How to extract a number from a string (not a file) in Bash

export hobba=ksjdjf123ksks export nokka=`sed “s/[^0-9]//g” <<< $hobba` echo $nokka 123 Sources: http://stackoverflow.com/questions/13055889/sed-with-a-literal-string-not-input-file, http://stackoverflow.com/questions/6388046/extract-integer-from-string-using-bash

Posted in Uncategorized | Tagged , , , , , | Leave a comment

Delete the last line in a text file using sed

sed -i ‘$d’ hobba -i: to change the file itself, not copy contents to another file or something$d: delete the last linehobba: the file name

| Tagged , | Leave a comment

How to delete certain lines of text from a file in Bash using sed

“sed -i 1,+2d file_name” deletes starting from line ‘1’ and the next ‘2’ lines, i.e. deletes the first 3 lines

| Tagged , , | Leave a comment

Extract Lines From a Text File

“sed -n 2,4p file_name” extracts lines from 2 to 4 from that file

| Tagged , | Leave a comment