Sed notes
From raju
grep till the end of paragraph
On a Debian system, rc-alert prints the release critical bugs in the installed packages. Sample output looks
% rc-alert Package: akonadi-server Bug: 804272 Title: fails to synchronize caldav to OpenXChange without notice Flags: [ U ] (upstream) Dists: [S] (stable) Package: akregator Bug: 836011 Title: akregator: Akregator keep crashing at exit, sometimes do not save recent feeds Flags: [P+ ] (pending, patch) Dists: [TU] (testing, unstable) Package: anacron Bug: 744753 Title: anacron: Anacron not triggered when system resumes under systemd Flags: [ +H ] (patch, help [wanted]) Dists: [STU] (stable, testing, unstable) Package: apt-xapian-index Bug: 793681 ...
If we are only interested in RC bugs of a single package, it can be specified as an argument. For example
% rc-alert synaptic Package: src:synaptic Bug: 831268 Title: synaptic: FTBFS: cacheset.h:718:15: error: 'make_unsigned' in namespace 'std' does not name a template type Flags: [ ] (none) Dists: [TU] (testing, unstable) Package: synaptic Bug: 797271 Title: Synaptic false positive, does not launch Flags: [ ] (none) Dists: [T] (testing) Package: synaptic Bug: 811857 Title: synaptic: FTBFS with GCC 6: no matching function for call to Flags: [ ] (none) Dists: [TU] (testing, unstable)
The same can also be achieved by parsing the output of the original command such that it displays stanzas corresponding to synaptic package.
% rc-alert | sed -n '/Package.*synaptic/,/^$/p' Package: src:synaptic Bug: 831268 Title: synaptic: FTBFS: cacheset.h:718:15: error: 'make_unsigned' in namespace 'std' does not name a template type Flags: [ ] (none) Dists: [TU] (testing, unstable) Package: synaptic Bug: 797271 Title: Synaptic false positive, does not launch Flags: [ ] (none) Dists: [T] (testing) Package: synaptic Bug: 811857 Title: synaptic: FTBFS with GCC 6: no matching function for call to Flags: [ ] (none) Dists: [TU] (testing, unstable)
Obviously "rc-alert synaptic" will be faster than parsing the output of "rc-alert". But I wanted to use this to demonstrate the capabilities of sed to parse up to the end of a paragraph.
% time rc-alert | sed -n '/Package.*synaptic/,/^$/p' > /dev/null rc-alert 2.22s user 0.04s system 77% cpu 2.901 total sed -n '/Package.*synaptic/,/^$/p' > /dev/null 0.00s user 0.00s system 0% cpu 2.901 total % time rc-alert synaptic > /dev/null rc-alert synaptic > /dev/null 0.19s user 0.02s system 12% cpu 1.713 total
replace lines matching a pattern with blank lines
sed -i -e 's/.*pattern.*//g' file.txt
To replace multiple patterns
sed -i -e 's/.*pattern1.*\|.*pattern2.*//g' file.txt
remove lines matching a pattern
sed -i -e '/pattern/d' file.txt
To remove lines matching multiple patterns
sed -i -e '/pattern1\|pattern2/d' file.txt
remove window line endings
sed -e 's/\x0d/\n/g' input.txt > output.txt
where '\x0d' is the hex code of '^M'. To find the hex code of a character in a file, open it in vim, press ga in normal mode. The hex code will be displayed in the status bar.
To do multiple replacements, chain them
sed -e 's/\x00/\n/g' -e 's/\x0d/\n/g' input.txt > output.txt
remove comments with leading whitespace
sed -e '/^[ \t]*#/d'
remove leading whitespace
sed -e 's/^[[:space:]]*//' sed -e 's/^[ \t]*//'
To remove leading spaces
sed -e 's/^ *//'