Common Commands for Sed on FreeBSD

Replace string:

% sed -i .bak 's/original/new/g' test.txt    
-i Edit files in-place, saving backups with the specified extension(here it is .bak).
 
Replace the comments of C language:
% sed 's/\(\/\*\).*\(\*\/\)//g' test.c

Delete the line contains pattern string:

% sed -i .bak '/pattern_string/d'  file.txt

Add new line after pattern string:

% sed -E 's/(pattern_string)/\1\nnew line/' file.txt

-E Interpret regular expressions as extended (modern) regular expressions rather    than basic regular expressions (BRE's).
\1 Represent pattern string.

Escape single quote:

% sed 's/ones/one\x27s/'
And string ones thing will become one's thing.

Copy specific lines in a file to another file:

copy lines 10 to 15 of file1.txt into file2.txt.
% sed -n '10,15p' file1.txt > file2.txt

Non-greedy matching for ssed (super sed).

The content of file test.txt was as follows:
ssABteAstACoABnnACss
 
Non-greed matching command as follows:
% ssed -R 's/AB.*?AC/XXX/g' test.txt
 
Operation on file test.txt directly(NO BACKUP):
% ssed -i -R 's/AB.*?AC/XXX/g' test.txt
 
Then,the content of filte test.txt is as follows:
ssXXXoXXXss
 

Comments

Popular posts from this blog

BdsDex: failed to load Boot0001 "UEFI BHYVE SATA DISK BHYVE-OABE-20A5-E582" from PciRoot(0x0)/Pci (0x2, 0x0)/Stat(0x0,0xFFFF,0x0) : Not Found

How To Install Nginx, MySQL and PHP (FEMP) Stack on FreeBSD 13.0

Install samba on FreeBSD(on VMware Workstation) to share files with Window.