_ _ ___ | |__ __ _ _______ | |_ / _ \| '_ \ / _` |_ / _ \| __| | (_) | | | | (_| |/ / (_) | |_ \___/|_| |_|\__,_/___\___/ \__|
| ohazot | docs | links | dev | conf | txt |
| es | en |
| mdoc file |
| search |
text manipulation —
tools
| sed | : text stream editor, commonly used for substitution. |
| cut | : split string. |
| tr | : translate characters. |
| paste | : join files/lines. |
| awk | : pattern processing language. |
SED
| replace old with new | : s/old/new/ |
| replace all occurrences | : s/old/new/g |
| replace the second occurrence | : s/l/p/2 |
Examples
$ echo "hello" | sed "s/l/p/" heplo $ echo "hello" | sed "s/l/p/g" heppo $ echo "hello" | sed "s/l/p/2" helpo
CUT
| -d | | delimeter |
| -c | | split by character count |
| -d | | split by character (specify delimeter) |
| -f | | specify field count. Use n,-n,n-
-f 2, gets 2nd field; -f -2, gets up to the 2nd
field; -f 2-, gets from the 2nd field to the end
|
$ echo "a b c" | cut -d " " -f 2 b $ echo "a b c" | cut -d " " -f 2- b c $ echo hello | cut -c 2 e
TR
$ echo "hello" | tr "l" "m" hemmo
control chacters
echo "hello" | tr 'e' '\n' h llo
echo 'hello' | tr 'l' '\t' he o
character range
echo 'hello' | tr 'h-l' 'a-d' aeddo
Character classes
echo '1.hello\n2.goodbye' | tr '[:digit:]' 'a-c' b.hello c.goodbye
PASTE
Requires a file on OpenBSD, cat be a pipe redirection on Slackware.
OpenBSD
$ cat test.log 1 2 3 $ paste -s -d "+" test.log 1+2+3
Slackware
cat test.log | paste -s -d + 1+2+3
awk
| awk '{print $$0};/^.Sh [A-Z]/{ shc++; if ( shc > 1 ) { print ".Lk #back_to_top ${${@}_TOP_MSG}0}}' .Sh TODO (back to top)
- review and improve if needed.