_ _
___ | |__ __ _ _______ | |_
/ _ \| '_ \ / _` |_ / _ \| __|
| (_) | | | | (_| |/ / (_) | |_
\___/|_| |_|\__,_/___\___/ \__|
text manipulation —
tools
| sed |
: text stream editor, commonly used for substitution. |
| cut |
: split string. |
| tr |
: translate characters. |
| paste |
: join files/lines. |
| replace old with new |
: s/old/new/ |
| replace all occurrences |
: s/old/new/g |
| replace the second occurrence |
: s/l/p/2 |
$ echo "hello" | sed "s/l/p/"
heplo
$ echo "hello" | sed "s/l/p/g"
heppo
$ echo "hello" | sed "s/l/p/2"
helpo
back to top
| -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
back to top
$ echo "hello" | tr "l" "m"
hemmo
echo "hello" | tr 'e' '\n'
h
llo
echo 'hello' | tr 'l' '\t'
he o
echo 'hello' | tr 'h-l' 'a-d'
aeddo
echo '1.hello\n2.goodbye' | tr '[:digit:]' 'a-c'
b.hello
c.goodbye
back to top
Requires a file OpenBSD, cat be a pipe redirection on
Slackware.
$ cat test.log
1
2
3
$ paste -s -d "+" test.log
1+2+3
cat test.log | paste -s -d +
1+2+3