### Compare ^ and \` for start of pattern space Source: https://www.gnu.org/software/sed/manual/html_node/regexp-extensions.html Demonstrates how ^ matches the start of lines in multi-line mode, whereas \` matches only the absolute start of the pattern space. ```bash $ printf "a\nb\nc\n" | sed 'N;N;s/^/X/gm' Xa Xb Xc $ printf "a\nb\nc\n" | sed 'N;N;s/\`/X/gm' Xa b c ``` -------------------------------- ### Perform substitution on every 3rd line with sed first~step Source: https://www.gnu.org/software/sed/manual/html_node/Common-Commands.html GNU sed provides an extension address syntax of first~step to achieve the same result as using two n commands. This example substitutes every 3rd line starting from line 0. ```bash $ seq 6 | sed '0~3s/./x/' 1 2 x 4 5 x ``` -------------------------------- ### Match beginning of a word with \< Source: https://www.gnu.org/software/sed/manual/html_node/regexp-extensions.html Matches the start of a word sequence. ```bash $ echo "abc %-= def." | sed 's/\/ ; s/$/\n<--END/' input.txt START--> a a a aa aaa aaaa aaaa aa aaaa aaa aaa <--END START--> bbbb bbb bbb bb bb bbb bb bbbbbbbb bbb <--END START--> ccc ccc cccc cccc ccccc c cc cc cc cc <--END ``` -------------------------------- ### Match lines starting with 'b', any character, then 'd' using sed Source: https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions-Overview.html This command uses the caret (^) to match the beginning of the line and the dot (.) to match any single character. It prints lines that start with 'b', followed by any character, followed by 'd'. The output shows the matching lines: bad, bed, bid, and body. ```shell $ printf "%s\n" abode bad bed bit bid byte body | sed -n '/^b.d/p' bad bed bid body ``` -------------------------------- ### Example of a poor bug report Source: https://www.gnu.org/software/sed/manual/html_node/Reporting-Bugs.html An example of an unhelpful bug report that lacks a stand-alone test case. ```text __while building frobme-1.3.4__ $ configure error→ sed: file sedscr line 1: Unknown option to 's' ``` -------------------------------- ### Difference between 1,/regexp/ and 0,/regexp/ Source: https://www.gnu.org/software/sed/manual/html_node/Range-Addresses.html These two commands demonstrate the difference between starting a range with address 1 and address 0. With 1,/regexp/, the range spans to the second occurrence of the regexp; with 0,/regexp/, it ends at the first occurrence if it matches the first line. ```shell $ seq 10 | sed -n '1,/[0-9]/p' 1 2 ``` ```shell $ seq 10 | sed -n '0,/[0-9]/p' 1 ``` -------------------------------- ### Read file content with r command Source: https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html Example of using the 'r' command to read and insert the contents of a file into the output stream. ```bash $ seq 3 | sed '2r/etc/hostname' 1 2 fencepost.gnu.org 3 ``` -------------------------------- ### Equivalent sed commands with different delimiters Source: https://www.gnu.org/software/sed/manual/html_node/Regexp-Addresses.html Three equivalent sed commands that print lines starting with '/home/alice/documents/'. The first uses escaped slashes, the second uses % as delimiter, and the third uses ; as delimiter. Custom delimiters avoid escaping slashes. ```bash sed -n '/^\/home\/alice\/documents\//p' sed -n '\%^/home/alice/documents/%p' sed -n '\;^/home/alice/documents/;p' ``` -------------------------------- ### Case conversion with empty substitution Source: https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html Example showing how \u affects the remainder of the replacement text when followed by an empty substitution. ```sed s/\(b\?\)-/\u\1x/g ``` -------------------------------- ### a command: GNU extension with -e parameters Source: https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html As a GNU extension, the 'a' command and text can be separated into two -e parameters. The second example shows using a shell variable. ```shell $ seq 3 | sed -e '2a\' -e hello 1 2 hello 3 ``` ```shell $ sed -e '2a\' -e "$VAR" ``` -------------------------------- ### i command: GNU extension with -e parameters Source: https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html As a GNU extension, the 'i' command and text can be separated into two -e parameters. The second example shows using a shell variable. ```shell $ seq 3 | sed -e '2i\' -e hello 1 hello 2 3 ``` ```shell $ sed -e '2i\' -e "$VAR" ``` -------------------------------- ### Separate simple sed commands with semicolons Source: https://www.gnu.org/software/sed/manual/html_node/Multiple-commands-syntax.html Demonstrates using a semicolon to separate simple sed commands, producing the same output as the newline and -e examples. ```shell $ seq 6 | sed '1d;3d;5d' 2 4 6 ``` -------------------------------- ### Perform substitution then print the second input line with sed group Source: https://www.gnu.org/software/sed/manual/html_node/Common-Commands.html A group of commands may be enclosed between { and } characters. This example uses a group to perform a substitution and then print the second input line, with auto-print disabled. ```bash $ seq 3 | sed -n '2{s/2/X/ ; p}' X ``` -------------------------------- ### c command: GNU extension with -e parameters Source: https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html As a GNU extension, the 'c' command and text can be separated into two -e parameters. The second example shows using a shell variable. ```shell $ seq 3 | sed -e '2c\' -e hello 1 hello 3 ``` ```shell $ sed -e '2c\' -e "$VAR" ``` -------------------------------- ### Use semicolons with {, }, b, t, T, : commands (GNU extension) Source: https://www.gnu.org/software/sed/manual/html_node/Multiple-commands-syntax.html Shows that the {, }, b, t, T, : commands can be separated with semicolons, a non-portable GNU sed extension. The first example uses braces, the second combines a braced block with a following command. ```shell $ seq 4 | sed '{1d;3d}' 2 4 $ seq 6 | sed '{1d;3d};5d' 2 4 6 ``` -------------------------------- ### Quit with status 42 on pattern match Source: https://www.gnu.org/software/sed/manual/html_node/sed-script-overview.html Uses a regular expression address /^foo/ and the quit command q with option 42. If a line starting with 'foo' is found, sed exits with status 42; otherwise it exits with status 0. ```bash sed '/^foo/q42' input.txt > output.txt ``` -------------------------------- ### Print first 10 lines with sed Source: https://www.gnu.org/software/sed/manual/html_node/head.html Displays the first 10 lines of input. The number of lines to print is the number before the 'q' command. ```sed #!/usr/bin/sed -f 10q ``` -------------------------------- ### Delete the second input line with sed d Source: https://www.gnu.org/software/sed/manual/html_node/Common-Commands.html The d command deletes the pattern space and immediately starts the next cycle. This example deletes the second input line. ```bash $ seq 3 | sed 2d 1 3 ``` -------------------------------- ### Add a header file to the beginning of all source files Source: https://www.gnu.org/software/sed/manual/html_node/Adding-a-header-to-multiple-files.html Creates a header file LIC.TXT using a here-document, then uses sed -i '0rLIC.TXT' to insert the file at the beginning of all .cpp and .h files. The 0 address with r requires GNU sed 4.9 or later. ```bash ## Create the header file: $ cat<<'EOF'>LIC.TXT /* Copyright (C) 1989-2021 FOO BAR This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; If not, see . */ EOF ## Add the file at the beginning of all source code files: $ sed -i '0rLIC.TXT' *.cpp *.h ``` -------------------------------- ### Initialize sed script Source: https://www.gnu.org/software/sed/manual/html_node/uniq.html The script begins by using the h command. ```sed #!/usr/bin/sed -f h ``` -------------------------------- ### Script configuration variables Source: https://www.gnu.org/software/sed/manual/html_node/Rename-files-to-lower-case.html Initial configuration for command execution and file finding. ```bash apply_cmd='sh' finder='echo "$@" | tr " " "\n"' files_only= ``` -------------------------------- ### Join lines starting with whitespace using sed -E ':a ; $!N ; s/\n\s+/ / ; ta ; P ; D' Source: https://www.gnu.org/software/sed/manual/html_node/Joining-lines.html Joins lines that begin with whitespace, as in SMTP headers. The example shows input file 2.txt and the command output. A portable (non-GNU) variation is provided in a comment. ```bash $ cat 2.txt Subject: Hello World Content-Type: multipart/alternative; boundary=94eb2c190cc6370f06054535da6a Date: Tue, 3 Jan 2017 19:41:16 +0000 (GMT) Authentication-Results: mx.gnu.org; dkim=pass header.i=@gnu.org; spf=pass Message-ID: From: John Doe To: Jane Smith $ sed -E ':a ; $!N ; s/\n\s+/ / ; ta ; P ; D' 2.txt Subject: Hello World Content-Type: multipart/alternative; boundary=94eb2c190cc6370f06054535da6a Date: Tue, 3 Jan 2017 19:41:16 +0000 (GMT) Authentication-Results: mx.gnu.org; dkim=pass header.i=@gnu.org; spf=pass Message-ID: From: John Doe To: Jane Smith # A portable (non-gnu) variation: # sed -e :a -e '$!N;s/\n */ /;ta' -e 'P;D' ``` -------------------------------- ### Execution of wrap40.sed Source: https://www.gnu.org/software/sed/manual/html_node/Line-length-adjustment.html The command to run the wrap40.sed script on the input file and the resulting wrapped output. ```bash $ sed -E -f wrap40.sed two-cities-mix.txt It was the best of times, it was the wor st of times, it was the age of wisdom, i t was the age of foolishness, ``` -------------------------------- ### Input file two-cities-mix.txt Source: https://www.gnu.org/software/sed/manual/html_node/Line-length-adjustment.html The sample input file containing text to be wrapped. ```text $ cat two-cities-mix.txt It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, ``` -------------------------------- ### Shell script header and usage documentation Source: https://www.gnu.org/software/sed/manual/html_node/Rename-files-to-lower-case.html The initial script header defining usage patterns for move-to-lower and move-to-upper. ```bash #! /bin/sh # rename files to lower/upper case... # # usage: # move-to-lower * # move-to-upper * # or # move-to-lower -R . # move-to-upper -R . # ``` -------------------------------- ### Step address usage: select every 3rd line starting from 1 Source: https://www.gnu.org/software/sed/manual/html_node/Numeric-Addresses.html Uses the GNU step address `1~3` to print every 3rd line starting from line 1. The `-n` option suppresses automatic printing, and `p` prints the selected lines. Output shows lines 1, 4, 7, and 10. ```shell $ seq 10 | sed -n '1~3p' 1 4 7 10 ``` -------------------------------- ### Basic sed invocation syntax Source: https://www.gnu.org/software/sed/manual/html_node/Command_002dLine-Options.html The standard format for running the sed command. ```bash sed OPTIONS... [SCRIPT] [INPUTFILE...] ``` -------------------------------- ### Reverse line order logic Source: https://www.gnu.org/software/sed/manual/html_node/tac.html Appends the buffer to the current line starting from the second line to reverse the order. ```sed # from the second line, the buffer (which contains all previous lines) # is *appended* to current line, so, the order will be reversed 1! G ``` -------------------------------- ### Match words with repeating characters using back-references Source: https://www.gnu.org/software/sed/manual/html_node/Back_002dreferences-and-Subexpressions.html Matches words starting with any character, followed by 'o', and ending with the same initial character. ```bash $ sed -E -n '/^(.)o\1$/p' /usr/share/dict/words bob mom non pop sos tot wow ``` -------------------------------- ### POSIX-conforming N command behavior Source: https://www.gnu.org/software/sed/manual/html_node/Reporting-Bugs.html Demonstrates how to force POSIX-conforming behavior for the N command using the --posix flag. ```bash $ seq 3 | sed --posix N 1 2 ``` -------------------------------- ### Find files and add header only to those without copyright Source: https://www.gnu.org/software/sed/manual/html_node/Adding-a-header-to-multiple-files.html Combines find, xargs, grep, and sed to locate .cpp, .c, and .h files in all subdirectories, filter out files that already contain 'copyright' (case-insensitive), and insert the header file LIC.TXT at the beginning of the remaining files. ```bash find \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \) \ | xargs grep -Li copyright \ | xargs -r sed -i '0rLIC.TXT' ``` -------------------------------- ### Case conversion with g flag Source: https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html Example of using the s command with the g flag and the \u sequence, where case conversion does not propagate between matches. ```sed s/\(b\?\)-/x\u\1/g ``` -------------------------------- ### Equivalent ways to read from standard input Source: https://www.gnu.org/software/sed/manual/html_node/Overview.html Three equivalent commands that filter standard input: using input redirection, piping from cat, and using '-' as an input file. ```bash sed 's/hello/world/g' input.txt > output.txt sed 's/hello/world/g' < input.txt > output.txt cat input.txt | sed 's/hello/world/g' - > output.txt ``` -------------------------------- ### c\ command: new cycle behavior Source: https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html Shows that the 'c' command starts a new cycle, so the substitution command is not performed on the replaced text. ```shell $ seq 3 | sed '2c\ hello s/./X/' -|X -|hello -|X ``` -------------------------------- ### Equivalent script specification methods Source: https://www.gnu.org/software/sed/manual/html_node/Overview.html Shows equivalent ways to specify the sed script: directly, with -e, with --expression, with -f, and with --file. ```bash sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt ``` -------------------------------- ### sed command syntax template Source: https://www.gnu.org/software/sed/manual/html_node/sed-script-overview.html Shows the general syntax for a sed command: optional address, single-letter command, and optional options. ```sed [addr]X[options] ``` -------------------------------- ### Equivalent sed commands for delete and substitute Source: https://www.gnu.org/software/sed/manual/html_node/sed-script-overview.html Four equivalent ways to delete lines matching /^foo/ and replace 'hello' with 'world': using semicolons, multiple -e options, a script file, and a combination of -e and -f. ```bash sed '/^foo/d ; s/hello/world/g' input.txt > output.txt ``` ```bash sed -e '/^foo/d' -e 's/hello/world/g' input.txt > output.txt ``` ```bash echo '/^foo/d' > script.sed echo 's/hello/world/g' >> script.sed sed -f script.sed input.txt > output.txt ``` ```bash echo 's/hello/world/g' > script2.sed sed -e '/^foo/d' -f script2.sed input.txt > output.txt ``` -------------------------------- ### y command: transliterate characters Source: https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html Transliterates characters in the pattern space. The example maps 'a-j' to '0-9'. The '/' delimiter can be replaced by any single character, and source/dest lists must have equal length after de-escaping. ```shell $ echo hello world | sed 'y/abcdefghij/0123456789/' 74llo worl3 ``` -------------------------------- ### Equivalent grep command for 'hello' Source: https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions-Overview.html This grep command is equivalent to the sed command above, printing lines that contain the string 'hello'. ```shell grep 'hello' ``` -------------------------------- ### Help function definition Source: https://www.gnu.org/software/sed/manual/html_node/Rename-files-to-lower-case.html The help function displaying usage instructions for the script. ```bash help() { cat << eof Usage: $0 [-n] [-r] [-h] files... ``` ```bash -n do nothing, only see what would be done -R recursive (use find) -h this message files files to remap to lower case ``` ```bash Examples: $0 -n * (see if everything is ok, then...) $0 * ``` ```bash $0 -R . ``` ```bash eof } ``` -------------------------------- ### c\ command: replace lines with multi-line text Source: https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html Deletes lines matching address range and outputs the following text. A new cycle is started after this command, so subsequent commands are not applied to the replaced text. ```shell $ seq 5 | sed '2,4c\ hello\ world' -|1 -|hello -|world -|5 ``` -------------------------------- ### Replace on lines matching a regex: sed '/apple/s/hello/world/' Source: https://www.gnu.org/software/sed/manual/html_node/Addresses-overview.html Uses a regular expression address '/apple/' to match lines containing the string 'apple', and applies the substitution only on those lines. ```bash sed '/apple/s/hello/world/' input.txt > output.txt ``` -------------------------------- ### Join specific lines with sed '2{N;s/\n//;}' Source: https://www.gnu.org/software/sed/manual/html_node/Joining-lines.html Joins lines 2 and 3 by reading line 2 with N and removing the newline. The example shows input file lines.txt and the command output. ```bash $ cat lines.txt hello hel lo hello $ sed '2{N;s/\n//;}' lines.txt hello hello hello ``` -------------------------------- ### Insert multiple lines at the beginning using \n Source: https://www.gnu.org/software/sed/manual/html_node/Adding-a-header-to-multiple-files.html Uses sed -i '1i...' with \n to insert multiple lines at the beginning of all .c files. The -i option edits files in place. ```bash sed -i '1i/*\n * Copyright (C) FOO BAR\n * Created by Jane Doe\n */' *.c ``` -------------------------------- ### Input file jaques.txt with soft line breaks Source: https://www.gnu.org/software/sed/manual/html_node/Branching-and-flow-control.html Sample quoted-printable file with lines split and marked with a soft line break (a single '=' at the end of the line). Used as input for the sed branching examples. ```text $ cat jaques.txt All the wor= ld's a stag= e, And all the= men and wo= men merely = players: They have t= heir exits = and their e= ntrances; And one man= in his tim= e plays man= y parts. ``` -------------------------------- ### Basic sed invocation syntax Source: https://www.gnu.org/software/sed/manual/html_node/Overview.html Shows the general form of a sed command: sed SCRIPT INPUTFILE... ```bash sed SCRIPT INPUTFILE... ``` -------------------------------- ### Labels with semicolons and portable equivalent Source: https://www.gnu.org/software/sed/manual/html_node/Multiple-commands-syntax.html Shows that labels in b, t, T, : commands are read until a semicolon, with leading and trailing whitespace ignored. The first example works with GNU sed; the second is a portable equivalent using -e options. ```shell $ seq 3 | sed '/1/b x ; s/^/=/ ; :x ; 3d' 1 =2 $ seq 3 | sed -e '/1/bx' -e 's/^/=/' -e ':x' -e '3d' 1 =2 ``` -------------------------------- ### Insert command with /b/ address and i command Source: https://www.gnu.org/software/sed/manual/html_node/Regexp-Addresses.html Shows the difference between the I modifier and the i command. Here /b/ is the address, i is the insert command, and d is the value to insert. A line with 'd' is inserted above the matched line. ```bash $ printf "%s\n" a b c | sed '/b/id' a d b c ``` -------------------------------- ### Infinite loop with b command and label Source: https://www.gnu.org/software/sed/manual/html_node/Branching-and-flow-control.html The b command jumps to the 'x' label, preventing a new cycle from starting, so the program never terminates and prints nothing. Requires GNU sed; a portable equivalent is provided in comments. ```shell $ seq 3 | sed ':x ; bx' # The above command requires gnu sed (which supports additional # commands following a label, without a newline). A portable equivalent: # sed -e ':x' -e bx ``` -------------------------------- ### Print only the second input line with sed -n p Source: https://www.gnu.org/software/sed/manual/html_node/Common-Commands.html The p command prints the pattern space to standard output. It is usually used with the -n command-line option to suppress auto-print. This example prints only the second input line. ```bash $ seq 3 | sed -n 2p 2 ``` -------------------------------- ### Replace hello with world in input.txt Source: https://www.gnu.org/software/sed/manual/html_node/Overview.html Uses the s command with the global flag to replace every 'hello' with 'world' in input.txt, writing the result to output.txt. ```bash sed 's/hello/world/g' input.txt > output.txt ``` -------------------------------- ### Format and print Source: https://www.gnu.org/software/sed/manual/html_node/cat-_002dn.html Formats the line number and prints the line. ```sed # Format it and print it s/^/ / s/^ *\(......\)\n/\1 /p ``` -------------------------------- ### Perform substitution on every 3rd line with sed n Source: https://www.gnu.org/software/sed/manual/html_node/Common-Commands.html The n command prints the pattern space if auto-print is not disabled, then replaces it with the next line of input. This example uses two n commands to skip two lines and then substitutes the third line. ```bash $ seq 6 | sed 'n;n;s/./x/' 1 2 x 4 5 x ``` -------------------------------- ### Add a header file after the first line of script files Source: https://www.gnu.org/software/sed/manual/html_node/Adding-a-header-to-multiple-files.html Creates a header file LIC.TXT with comment lines, then uses sed -i '1rLIC.TXT' to insert the file after the first line of all .py and .sh files, preserving the shebang line. ```bash ## Create the header file: $ cat<<'EOF'>LIC.TXT ## ## Copyright (C) 1989-2021 FOO BAR ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 3, or (at your option) ## any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see . ## ## EOF ## Add the file at the beginning of all source code files: $ sed -i '1rLIC.TXT' *.py *.sh ```