ADVANCED_USES.sed
# Search for multiple patterns + one
sed -rn '/(\[processnameA\]|\[processnameB\]).*error message one/p' infile
sed -n '/PATTERN1.*PATTERN2/p'
sed 's/'`echo "\033"`'/foo/g'
sed -i “s/\oXX/,/g” file
SED REPLACE A PATTERN ON LINES STARTING WITH…
sed '/^1/s/\(.\{4\}\)\(.\{9\}\)/\1123456789/'
To '/^1/….για lines starting with 1
REPLACE NON PRINTABLE CHARACTERS WITH SED (USING :PRINT:)
sed 's/[^[:print:]\r\t]/ /g' < in > out
REPLACE NON PRINTABLE 0x00 up to 0x1F except 0x09 (TAB) 0x0A(new line) 0x0D(CR)
sed -e 's/[\x00-\x08\x0B\x0C\x0E-\x1F]/ /g' $FILE_IN > $FILE_OUT
perl -p -e 's/[\x00-\x08\x0B\x0C\x0E-\x1F]/ /g'
REPLACE A PATTERN ON LINES ENDING WITH..
SED REPLACE PATTERNS USING COLUMNS / DIGITS
sed '/^1/s/\(.\{4\}\)\(.\{9\}\)/\1123456789/'
Change lines that start with 1, capture first 4, replace following 9
SED REPLACE PATTERNS USING COLUMNS / DIGITS 2
sed -r '/^1/s/(.{4})(.{9})/\1123456789/'
(Not portable)
SED REPLACE WHATEVER IS AFTER PATTERN WITH…
sed -i -e "s/BTCH_DATE_s0=.*/BTCH_DATE_s0='$s'/" /home/tede/.profile
SED WITH REGEX REPLACE AFTER PATTERN
sed -i 's/^user=[A-Za-z0-9_]*/user=bob/g' /tmp/myfile
SED AFTER PATTERN / ANY NUMBER OF CHARACTERS
sed -r 's/(Unprocessed\\X)[0-9]+/\1'"$a"'/' "sedtest"
SED WITH TWO PATTERNS AND REPLACING AFTER THEM
sed -e "s/\([ABDF][ALNDV]\)${datepattern}/\1${b}/"
SED WITH PATTERN AND REPLACING ONLY AFTER IT
sed -e "s/\(A[AL]\)${datepattern}/\1${b}/"
REMOVE ALL NON ASCII CHARACTERS FROM A FILE
sed -i 's/[\d128-\d255]//g' FILENAME
SED WITH REGULAR EXPRESSIONS (FIRST MATCH)
s/[0-3][0-9][0-1][0-9][0-3][0-9]/$a/
SED WITH REGULAR EXPRESSIONS (ALL MATCH)
s/[0-3][0-9][0-1][0-9][0-3][0-9]/$a/g
SED WITH PATTERN IN A VARIABLE
$datepattern = [0-3][0-9][0-1][0-9][0-3][0-9]
sed -e "s/D${datepattern}$/D${a}/
sed -e 's/'$(echo "octal-value")'/replace-word/g'
sed 's/'`echo "octal-value"`'/replace-word/g'
sed 's/'`echo "\033"`'/foo/g'
sed -i “s/\oXX/,/g” file
sed -i 's/[^a-zA-Z 0-9`~!@#$%^&*()_+\[\]\\{}|;'\'':",.\/<>?]//g' FILE
awk '{ sub("[^a-zA-Z0-9\"!@#$%^&*|_\[](){}", ""); print }' MYinputfile.txt > pipe_out_to_CONVERTED_FILE.txt
# Remove empty lines
sed -e '/^[[:space:]]*$/ d' \
# 2 lines
sed 'p' BSD
# 3 lines
sed 'p;p' BSD
# Search for pattern and print line numbers
sed -n /PATTERN/= sed -n '/pattern/{=;p}' file | sed '{N;s/\n/ /}'
perl -ne 'print "$.: $_" if /regex/' input.file
sed -e 's/[[:space:]][[:space:]]*/\n/g'
# Convert file into oen word per line
sed -e 's/[[:space:]]/\n/g' | grep -v '^$'
1. sed_reg_firstmatch.sed : Substitute first match found
2. sed_reg_allmatch.sed : Substitute all matches found
3. sed_pattern_var : Sed with variable in pattern
4. sed_replace_after : Sed replace after pattern
5. sed_replace_after_2 : Sed replace after 2 patterns
6. sed_replace_after : Sed replace after 1 pattern
7. sed_replace_line_start : Sed replace on lines starting with
8. sed_replace_digits : Sed replace with specific columns, digits
9. sed_replacehex : Sed replace hex and range of hexes
10. sed_replacenonprint : Sed replace non printable characters
11. remove_non_ascii : Sed to remove all non ascii from file
12. remove_non_ascii_non_print : Sed to remoev all non ascii, non printable characters
13. print_2_times : Print lines two times each / three times each
14. remove_empty_lines : Remove all empty lines
15. convert_nl.sed : Convert blanks to new lines
16. replace_octal.sed : Replace octal values with words
17. see_np.sed : See all non printable values
18. pattern_line.sed : Search for pattern and print line numbers
19. two_patterns.sed : Search for two patterns plus one