------------------------------------------------------------------------------------- / _ \ \_\(_)/_/ _//"\\_ JOHLEM.net / \ https://johlem.net/V1/topics/cheatsheet.php ------------------------------------------------------------------------------------- --- SED Replace all files full path with just the filename no directory $ echo "/home/ubuntu/temp/myfile.txt" | sed 's/.*\///' Delete all lines that have only white-space $ sed '/^$/d' input.txt Delete all non-printable characters $ echo "Hello$tabWorld" | sed 's/[^[:print:]]//g' If there is not a match delete the line $ sed '/agarder/!d' mytext.txt Deleting a range of lines $ sed '29,34d' testfile.txt Deleting lines other than the mentioned $ sed '29,34!d' testfile.txt add a blank line after every non-blank line $ sed G testfile.txt Search and Replacing a string using sed $ sed 's/danger/safety/' testfile.txt Replace a string on a particular line $ sed '4,9 s/red/blue/' testfile.txt Running multiple sed commands $ sed -e 's/red/blue/g' -e 's/sam/bob/' testfile.txt Making a backup copy before editing a file $ sed -i.bak -e 's/bob/sammy/g' testfile.txt Removing all commented lines & empty lines $ sed -e 's/#.*//;/^$/d' testfile.txt Removing all commented lines $ sed -e 's/#.*//' testfile.txt insert multiple empty lines in the output, pass multiple G arguments separated by the semi-colon (;) character. $ sed 'G;G' textfile.txt Perform Multiple Substitutions at Once sed 's/sam/bob/g;s/car/bus/gi' textfile.txt Replace all uppercase characters of the text with lowercase characters $ sed 's/\(.*\)/\L\1/' os.txt Add commas to numbers that have more than 3 digits $ echo "5098673" | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' Replaces tab character with 4 space characters $ echo -e "1\t2\t3" | sed $'s/\t/ /g' Replaces 4 consecutive space characters with tab character $ echo -e "1 2" | sed $'s/ /\t/g'