sed

Replace pattern with file content

Tag(s):

NOT WORK: String with ampersand.

Delete all the lines after PATTERN

Tag(s):

# To delete all the lines after PATTERN, use this
sed '/PATTERN/,$d' input.txt

https://nixtricks.wordpress.com/2013/01/09/sed-delete-the-lines-lying-in-between-two-patterns/

Get mutiple line text between 2 patterns

Tag(s):

# Note: PATTERN-1 & PATTERN-2 will be also included in your result.
sed -n '/PATTERN-1/,/PATTERN-2/p' input.txt

http://stackoverflow.com/questions/15685471/sed-get-text-between-lines

Replace multiple grep piping

Tag(s):

Replace multiple grep piping with the following because piping is expensive:

Delete matching line

Tag(s):

# Shell(bash3.2+)
while read -r line
do

Remove duplicates spaces

Tag(s):

echo "Hello   World" | tr -s ' '

Match and replace on the last line only

Tag(s):

# Process the last line
sed '$s/PATTERN/REPLACE/' input_file

Backup and directly edit file

Tag(s):

# Backup and edit input file in-place using -i.bck option
sed -i.bck 's/SEARCH-PATTERN/REPLACE-STRING' inputfile
# Original input file will be backup with file extension '.bck'.

Delete text between 2 patterns

Tag(s):

# Delete text between patterns, excluding the lines containing these patterns:
sed '/PATTERN-1/,/PATTERN-2/{//!d}' input.txt
 
 
# Delete text between patterns, including the lines containing these patterns:

Insert file content after match

Tag(s):

# Insert content of file2.txt into file1.txt after <tag> is found.
sed '/<tag>/ r file2.txt' file1.txt
# /r stands for reading in a file
 
# Append line after match

Pages

Subscribe to RSS - sed