# Extracting a field (column) with a custom delimiter <your_command> | cut -d'DELIMITER' -fN # Extracting a specific range of characters (fixed-width columns) <your_command> | cut -cSTART-END
sed
<your_command> | sed -E 's/REGEX/\1/p'
Insert
Insert lines
Insert to the specific line
echo -e 'Foo\nBar' | sed '2i\the new line\'# Foo\nthe new line\nBar
Add line to beginning and end
echo -e 'Foo\nBar' | sed '1 i\First line' echo -e 'Foo\nBar' | sed '$aEnd line'
Insert lines after match pattern
echo -e 'Foo\nBar' | sed '/Foo/a NewLine1\nNewLine2' echo -e 'Foo\nBar' | sed '/Foo/r add.txt'
Insert text to the beginning and end
Insert text to the beginning of each line
echo'foo' | sed 's/^/BeginText/'
Insert text to the end of each line
echo'foo' | sed 's/$/EndText/'
Insert text to the begining and end of each line
echo'foo' | sed 's/^/BeginText/' | sed 's/$/EndText/'
Insert a new line to the end of each line
echo -e 'Foo\nBar'| sed 's/$/\r\n/'
Replace
Replace first
echo"old names, old books" | sed 's/old/new/' # or echo"old names, old books" | sed '0,/old/{s/old/new/}'
Replace all
echo"old names, old books" | sed 's/old/new/g'
Remove
Remove matched lines
echo -e "Foo\nBar" | sed '/Foo/d'
Remove empty line
echo -e "Foo\n \nBar" | sed '/^\s*$/d' # or echo -e "Foo\n \nBar" | sed '/^[[:space:]]*$/d'
Remove comment /**/ or //
# reomve lines start with / or * sed '/^ *[*/]/d'
Remove n lines after a pattern
# including the line with the pattern echo -e "Line1\nLine2\nLine3\nLine4" | sed '/Line1/,+2d'# Line4
# excluding the line with the pattern echo -e "Line1\nLine2\nLine3\nLine4" | sed '/Line1/{n;N;d}'# Line1\nLine4
Remove all lines between two patterns
# including the line with the pattern sed '/pattern1/,/pattern2/d;' echo -e "Foo\nAAA\nBBB\nBar\nCCC" | sed '/Foo/,/Bar/d'# CCC
# excluding the line with the pattern sed '/pattern1/,/pattern2/{//!d;};' echo -e "Foo\nAAA\nBBB\nBar\nCCC" | sed '/Foo/,/Bar/{//!d;}'# Foo\nBar\nCCC