Common Examples of Using gsed on FreeBSD

gsed [-nefir] [action]

Options and parameters:

  • -n :  Use silent mode. In general gsed usage, all data from STDIN will generally be listed on the screen. But if the -n parameter is added, only the line (or action) that has been specially processed by gsed will be listed.

  • -e command :  Edit gsed actions directly in the command line mode.

  • -f  command_file :  Write the gsed action directly in a file, -f command_file can execute the gsed action in command_file.

  • -i  : Modify the content of the read file directly instead of outputting it on the screen.

  • -rThe action of gsed supports the grammar of extended formal expressions. (The default is basic regular expressions grammar).
Action description: [n1[,n2]]function
 
n1, n2  :  It doesn’t necessarily exist. It generally means "choose the number of lines to perform an action". For example, if my action needs to be performed between 10 and 20 lines, then "10,20[action behavior]".
 
The function has the following options:
  • a : Addition. After "a" can be followed by strings, and these strings will appear on a new line (the current next line).

  • c : Replace. "c" can be followed by strings, and these strings can replace the lines between n1 and n2!

  • d : Delete. 

  • i : Insert. Strings can be followed by "i", and these strings will appear on a new line (the current previous line).

  • p : Print. That is, a certain selected data will be printed out. Usually "p" will run with the parameter gsed -n .
  • s : Substitute. You can directly work on this example! The usual actions can be matched with regular expressions! For example, 1,20s/old/new/g is it!


1. Add/delete function by line

Example 1.  List the contents of /etc/passwd and print the line number. At the same time delete lines 2~5.
nl /etc/passwd | gsed -e '2,5d'

Just delete line 2.
nl /etc/passwd | gsed -e '2d'

Delete the 3rd to the last line.
nl /etc/passwd | gsed -e '3,$d'

Example 2. Add "drink tea" after the second line (that is, add it to the third line).
nl /etc/passwd | gsed -e '2a drink tea'

Add "drink tea" before the second line.
nl /etc/passwd | gsed -e '2i drink tea'

Example 3. Add two lines after the second line, such as "Drink tea or..." and "Drink beer".
nl /etc/passwd | gsed -e '2a Drink tea or ...\nDrink beer?'
 

2. Replacement and display function by behavior unit

Example 4. I want to replace the content of lines 2-5 with "No 2-5 number".
nl /etc/passwd | gsed '2,5c No 2-5 number'

Example 5. List only lines 5-7 in the /etc/passwd file. 
nl /etc/passwd | gsed -n '5,7p'

3. Partial data search and replace function.

gsed 's/old-string/new-string/g'

4. Modify file content directly

Example 6. Use sed to change the end of each line in  regular_express.txt if it is, then replace it with!
gsed -i 's/\.$/\!/g' regular_express.txt


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.