sed join lines together

sed ‘:a;/0$/{N;s/\n//;ba}’ In a loop (branch ba to label :a), if the current line ends in 0 (/0$/) append next line (N) and remove inner newline (s/\n//). awk: awk ‘{while(/0$/) { getline a; $0=$0 a; sub(/\n/,_) }; print}’ Perl: perl -pe ‘$_.=<>,s/\n// while /0$/’ bash: while read line; do if [ ${line: -1:1} != “0” … Read more

Sed to extract text between two strings

sed -n ‘/^START=A$/,/^END$/p’ data The -n option means don’t print by default; then the script says ‘do print between the line containing START=A and the next END. You can also do it with awk: A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines … Read more

Insert newline (\n) using sed

The sed on BSD does not support the \n representation of a new line (turning it into a literal n): $ echo “123.” | sed -E ‘s/([[:digit:]]*)\./\1\n next line/’ 123n next line GNU sed does support the \n representation: $ echo “123.” | gsed -E ‘s/([[:digit:]]*)\./\1\nnext line/’ 123 next line Alternatives are: Use a single … Read more

Add text at the end of each line

You could try using something like: sed -n ‘s/$/:80/’ ips.txt > new-ips.txt Provided that your file format is just as you have described in your question. The s/// substitution command matches (finds) the end of each line in your file (using the $ character) and then appends (replaces) the :80 to the end of each … Read more

Can I programmatically “burn in” ANSI control codes to a file using unix utils?

To display a file that contains ANSI sequences, less -r typescript Or, less -R typescript To remove ANSI and backspace sequences from a file, creating a clean newfile, try: sed -r ‘:again; s/[^\x08]\x08\x1b\[K//; t again; s/\x1b_[^\x1b]*\x1b[\]//g; s/\x1B\[[^m]*m//g’ typescript >newfile How it works -r This turns on extended regular expressions. (On BSD systems, -r should be … Read more