black electronics

Sed

Common sed commands for editing and reformatting text during pentests.

Sed is a stream editor that lets you search, replace, and reformat text without opening a file in an editor. It’s especially handy for cleaning up wordlists and extracted data during an engagement.

Terminal window
sed -n '2345p' flag19 # Print only line 2345 of a file (awk 'NR==2345' flag19 works too)
Terminal window
sed 's/,/\n/g' comma_separated.txt > one_per_line.txt # Put each comma-separated value on its own line
Terminal window
sed ':a;N;$!ba;s/\n/", "/g' subdomains.txt | sed 's/^/"/;s/$/"/' # Join every line into one comma-separated, quoted string
Terminal window
sed 's/\./\n/g' subdomains.txt > words.txt # Split text at every . and put each part on its own line
Terminal window
wget -q -O - https://example.com/docs | sed -n 's/.*<h4[^>]*>\(.*\)<\/h4>.*/\1/p' # Extract the text inside every <h4> tag from a downloaded page
Terminal window
find . -type f -exec sed -i 's/.*://' {} \; # Strip everything before the first colon on every line, in every file in the current tree
Terminal window
sed 's/[^[:print:]]//g' wordlist.txt | grep -E '^.{8}$' # Strip non-printable characters, then keep only 8-character words
Useful LinksPentest PayloadsCheat Sheets