good info: https://www.grymoire.com/Unix/Sed.html
- change multiple values at column position for multiple records
sed -e 's/./X/10' -e
'/^000/s/./X/18' -e '/^001/s/./X/819' input
- grep uppercase words preceded by '-' & followed by ','
grep -oP "\-\w[A-Z]+\w," xxx
* print lines between patterns(inclusive) & stop
sed -n '/^pattern1/,/^pattern2/p;/^pattern2/q'
* print lines between patterns(exclusive)
sed -n '1,/^pattern1/d;/^pattern2/,$d;p'
* count # of lines between patterns (EMPLID)
awk '/EMPLID/ {if (count) print count; print; count=0; next} {count++} END {print count}' file
* replace char if pattern found
sed '/^Box 14/ s/#/^/g' file
*insert line after pattern
sed '/pattern/a insert_text_line' file
*insert line before pattern
(https://fabianlee.org/2018/10/28/linux-using-sed-to-insert-lines-before-or-after-a-match/)
* print lines between patterns(inclusive) & stop
sed -n '/^pattern1/,/^pattern2/p;/^pattern2/q'
* print lines between patterns(exclusive)
sed -n '1,/^pattern1/d;/^pattern2/,$d;p'
* count # of lines between patterns (EMPLID)
awk '/EMPLID/ {if (count) print count; print; count=0; next} {count++} END {print count}' file
* replace char if pattern found
sed '/^Box 14/ s/#/^/g' file
*insert line after pattern
sed '/pattern/a insert_text_line' file
*insert line before pattern
(https://fabianlee.org/2018/10/28/linux-using-sed-to-insert-lines-before-or-after-a-match/)
sed '/^anothervalue=.*/i before=me' test.txt
insert multiple lines
echo '123 pattern 456' | sed 's/\<pattern\>/xxx&xxx/'
echo '123 pattern 456' | sed 's/\(pattern\)/xxx\1xxx/'
echo '123 pattern 456' | sed 's/\(pattern\) 4\(.*\)6/xxx\1xxx \2/'
* replace extended ascii with spaces:
* print from match to end of file:
> sed -ne '/pattern/,$ p'
# alternatively, if you don't want to print the match:
> sed -e '1,/pattern/ d'
* Branching to Label:
> echo -e "a\nb\nc\nd\ne\nf\ng\nh" |
https://unix.stackexchange.com/questions/117511/is-it-possible-to-match-multiple-specific-line-numbers-not-range-with-sed
sed '/^anothervalue=.*/i before=me\nbefore2=me2' test.txt* add text to pattern:
echo '123 pattern 456' | sed 's/\<pattern\>/xxx&xxx/'
echo '123 pattern 456' | sed 's/\(pattern\)/xxx\1xxx/'
echo '123 pattern 456' | sed 's/\(pattern\) 4\(.*\)6/xxx\1xxx \2/'
* replace extended ascii with spaces:
SET KK_CLOSE_FLAG =Â 'N' | LANG=C
sed 's/[\x80-\xFF]//g'
* print from match to end of file:
> sed -ne '/pattern/,$ p'
# alternatively, if you don't want to print the match:
> sed -e '1,/pattern/ d'
* Branching to Label:
> echo -e "a\nb\nc\nd\ne\nf\ng\nh" |
sed '3b action; /e/
{
p; b action;
}
; b end; : action; a \ lalala : end'
https://unix.stackexchange.com/questions/117511/is-it-possible-to-match-multiple-specific-line-numbers-not-range-with-sed