Lukáš Bařinka
man -f sed
sed
[OPTION]… {script-only-if-no-other-script} [input-file]…
-n
-e script
-f script-file
-i[SUFFIX]
-E, -r
-E
-n
-e script
-f script-file
-i[SUFFIX]
-E, -r
-E
#
p
d
q
Q
{ CMD ; CMD ... }
s
s/RE/text/flags
y
y/src/dst/
#
p
d
q
Q
{ CMD ; CMD ... }
s
s/RE/text/flags
y
y/src/dst/
s/RE/text/flags
&
= matching pattern space\1
… \9
= matching subexpression\l
, \L
= convert to lowercase (first/all)\u
, \U
= convert to uppercase (first/all)\E
= stop case conversion&
= odpovídající řetězec (pattern space)\1
… \9
= odpovídající podvýraz\l
, \L
= převod na malá písmena (první/vše)\u
, \U
= převod na velká písmena (první/vše)\E
= konec konverze
s/RE/text/
s,RE,t/e/x/t,
/RE/s//text/
/RE/s///
s/[[:digit:]]+/(&)/
s/[[:digit:]]+/#&/
s/([[:digit:]]*)\.?([[:digit:]]*)/int: \1, frac: \2/
s/.*/L: \L&\E, U: \U&\E, orig: &/
g
p
w FILE
e
sed 's/$/ +%T/e; s/:/|/g' <<<date
Animation
#!/bin/sed -nf
/<h3/ {
# Date and number of exchange rate
s/.*Platnost od //
s/P.*: /#/
s/<.*//
# Another solution using subexpressions
#s/.*Platnost od \([^ ]*\) .*: \([0-9]\{1,\}\).*/\1 #\2/p
}
...
#!/bin/sed -nf
/<h3/ {
# Datum a pořadí kurzovního lístku
s/.*Platnost od //
s/P.*: /#/
s/<.*//
# Jinak, pomocí podvýrazů
#s/.*Platnost od \([^ ]*\) .*: \([0-9]\{1,\}\).*/\1 #\2/p
}
...
<br class="noprint">
<div class="highlighted" style="color:red; font-weight:bold;">Data pro aktuální pracovní den jsou k dispozici po 14:30</div><br>
<h3 class="kurzy_tisk">Platnost od 24.03.2010 Pořadí: 58</h3>
<table class="kurzy_tisk">
<tr><th>země</th><th>měna</th><th>množství</th><th>kód</th><th>kurz</th></tr>
<tr><td>Austrálie</td><td>dolar</td><td align="right">1</td><td>AUD</td><td align="right">17,313</td></tr><tr><td>Brazílie</td><td>real</td><td align="right">1</td><td>BRL</td><td align="right">10,621</td></tr><tr><td>Bulharsko</td><td>lev</td><td align="right">1</td><td>BGN</td><td align="right">12,976</td></tr><tr><td>Čína</td><td>renminbi</td><td align="right">1</td><td>CNY</td><td align="right">2,786</td></tr><tr><td>Dánsko</td><td>koruna</td><td align="right">1</td><td>DKK</td><td align="right">3,410</td></tr><tr><td>EMU</td><td>euro</td><td align="right">1</td><td>EUR</td><td align="right">25,375</td></tr><tr><td>Estonsko</td><td>koruna</td><td align="right">1</td><td>EEK</td><td align="right">1,622</td></tr><tr><td>Filipíny</td><td>peso</td><td align="right">100</td><td>PHP</td><td align="right">41,734</td></tr><tr><td>Hongkong</td><td>dolar</td><td align="right">1</td><td>HKD</td><td align="right">2,451</td></tr><tr><td>Chorvatsko</td><td>kuna</td><td align="right">1</td><td>HRK</td><td align="right">3,494</td></tr><tr><td>Indie</td><td>rupie</td><td align="right">100</td><td>INR</td><td align="right">41,826</td></tr><tr><td>Indonesie</td><td>rupie</td><td align="right">1000</td><td>IDR</td><td align="right">2,084</td></tr><tr><td>Japonsko</td><td>jen</td><td align="right">100</td><td>JPY</td><td align="right">20,692</td></tr><tr><td>Jihoafrická rep.</td><td>rand</td><td align="right">1</td><td>ZAR</td><td align="right">2,576</td></tr><tr><td>Jižní Korea</td><td>won</td><td align="right">100</td><td>KRW</td><td align="right">1,671</td></tr><tr><td>Kanada</td><td>dolar</td><td align="right">1</td><td>CAD</td><td align="right">18,590</td></tr><tr><td>Litva</td><td>litas</td><td align="right">1</td><td>LTL</td><td align="right">7,349</td></tr><tr><td>Lotyšsko</td><td>lat</td><td align="right">1</td><td>LVL</td><td align="right">35,845</td></tr><tr><td>Maďarsko</td><td>forint</td><td align="right">100</td><td>HUF</td><td align="right">9,611</td></tr><tr><td>Malajsie</td><td>ringgit</td><td align="right">1</td><td>MYR</td><td align="right">5,733</td></tr><tr><td>Mexiko</td><td>peso</td><td align="right">1</td><td>MXN</td><td align="right">1,518</td></tr><tr><td>MMF</td><td>SDR</td><td align="right">1</td><td>XDR</td><td align="right">28,830</td></tr><tr><td>Norsko</td><td>koruna</td><td align="right">1</td><td>NOK</td><td align="right">3,161</td></tr><tr><td>Nový Zéland</td><td>dolar</td><td align="right">1</td><td>NZD</td><td align="right">13,338</td></tr><tr><td>Polsko</td><td>zlotý</td><td align="right">1</td><td>PLN</td><td align="right">6,526</td></tr><tr><td>Rumunsko</td><td>nové leu</td><td align="right">1</td><td>RON</td><td align="right">6,237</td></tr><tr><td>Rusko</td><td>rubl</td><td align="right">100</td><td>RUB</td><td align="right">64,038</td></tr><tr><td>Singapur</td><td>dolar</td><td align="right">1</td><td>SGD</td><td align="right">13,538</td></tr><tr><td>Švédsko</td><td>koruna</td><td align="right">1</td><td>SEK</td><td align="right">2,618</td></tr><tr><td>Švýcarsko</td><td>frank</td><td align="right">1</td><td>CHF</td><td align="right">17,764</td></tr><tr><td>Thajsko</td><td>baht</td><td align="right">100</td><td>THB</td><td align="right">58,768</td></tr><tr><td>Turecko</td><td>nová lira</td><td align="right">1</td><td>TRY</td><td align="right">12,310</td></tr><tr><td>USA</td><td>dolar</td><td align="right">1</td><td>USD</td><td align="right">19,020</td></tr><tr><td>Velká Británie</td><td>libra</td><td align="right">1</td><td>GBP</td><td align="right">28,364</td></tr>
</table>
...
/<table.*kurzy_tisk/,+2 {
s/<\/tr>/\n/g # Rozdělit na řádky (multiline) ! GNU sed
s/<\/t[hd]>/|/g # Oddělit položky
s/<[^>]*>//g # Odstranit zbylé tagy
s/|\n/\n/g # Odstranit ukončení poslední položky
s/\r// # Odstranit (DOS) LF
s/\n$// # Odstranit odřádkování na koncích řádků původní tabulky
/^$/!p # Vypsat vše kromě prázdných řádků
}
...
/<table.*kurzy_tisk/,+2 {
s/<\/tr>/\n/g # Split into separate lines (multiline) ! GNU sed
s/<\/t[hd]>/|/g # Separate fields (table cells)
s/<[^>]*>//g # Remove remaining tags
s/|\n/\n/g # Remove trailing separator
s/\r// # Remove (DOS) LF
s/\n$// # Remove trailing newlines (of origin table)
/^$/!p # Print out everything except empty lines
}
URL='https://www.cnb.cz/en/financial-markets/foreign-exchange-market/central-bank-exchange-rate-fixing/central-bank-exchange-rate-fixing/'
curl "$URL" 2>/dev/null | sed ... | diff - rates.txt
URL='https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/index.html'
curl "$URL" 2>/dev/null | sed ... | diff - rates.txt
i TEXT
a TEXT
c TEXT
r FILE
R FILE
w FILE
W FILE
seq 3 | sed '2i text'
seq 3 | sed '2i\
text'
seq 3 | sed -e '2i\' -e text
seq 3 | sed -e '2i\' -e "$VAR"
seq 3 | sed '2i\
\text\
foo'
seq 3 | sed '2i\
\\text\\'
printf '%d %d %d\n' {1..15} \
| sed -E '
1 i <table>
i <tr><td>
s_ +_</td><td>_g
a </td></tr>
$ a </table>
'
info -n 'other commands' sed
man -k print \
| sed '
s_[^(]*(\([^)]*\)).*_\1_
' \
| sort -u \
| sed '
1i #!/bin/sed -f
s_.*_/(&)/w &.man_
' \
> mansplit
chmod +x mansplit
man -k print | ./mansplit
a2ps (1) - format files for printing on a PostScript printer
arch (1) - print machine hardware name (same as uname -m)
asprintf (3) - print to allocated string
B::Concise (3perl) - Walk Perl syntax tree, printing concise info about ops
B::Terse (3perl) - Walk Perl syntax tree, printing terse info about ops
bbox (1) - prints out the bounding box of a rawppm or rawpbm image
blkid (8) - locate/print block device attributes
card (1) - print reference card of program options
cat (1) - concatenate files and print on the standard output
cloudprint (1) - share CUPS printers with Google Cloud Print
cloudprint-service (7) - manage the Google Cloud Print proxy
cloudprintd (1) - share CUPS printers with Google Cloud Print
cps-auth (1) - Perform OAuth2 authentication for cloudprint-service
cups (1) - a standards-based, open source printing system
cups-browsed (8) - A daemon for browsing the Bonjour broadcasts of shared...
cups-calibrate (8) - ESP CUPS Printer Calibration Tool
cups-genppdupdate (8) - update CUPS+Gutenprint PPD files
cups-lpd (8) - receive print jobs and report printer status to lpd cl...
cupsdisable (8) - stop/start printers and classes
cupsenable (8) - stop/start printers and classes
Data::Dump (3pm) - Pretty printing of data structures
Data::Dump::Filtered (3pm) - Pretty printing with filtering
Data::Dumper (3perl) - stringified perl data structures, suitable for both pr...
date (1) - print or set the system date and time
n
-n
)n
-n
)
printf '%d\n' {1..10} | sed 'n;d'
1
3
5
7
9
printf '%d\n' {1..10} | sed '1!n;d'
2
4
6
8
10
h
/ H
g
/ G
x
stat -c '%s' * \
| sort -n \
| sed -n '
# at the beginning - store min
1 h
# at the end
$ {
# store \n + max
H
# load min + \n + max
g
# replace \n with :
s_\n_:_p
}'
input P.B. H.B.
first
first → first
first first → first
... first
... → ... first
last first
last → last first
last last → first\nlast
last first\nlast ← first\nlast
last first:last first\nlast
↓
tac
)Opačné pořadí řádek (tac
)
seq 100 | sed -n 'H;g;1,9!s/[^\n]*\n//;h;$p'
#!/bin/sed -n
H
g
1,9! s/[^\n]*\n//
h
$ p
D
N
P
G
H
seq 5 | sed -n 'N; h; s/\n/ - /p; g; D'
1 - 2
2 - 3
3 - 4
4 - 5
echo 1 2 3 4 5 6 7 8 9 10 | sed 's/ /\n/g; s/^/./gM ; P; D'
.1
..2
...3
....4
.....5
......6
.......7
........8
.........9
..........10
d
D
ADDR commandpříkaz
ADDR { commandspříkazy }
if-then
)if-then
): label
b [label]
t [label]
s///
command has succeededT [label]
t
command
echo 1234567890 | \
sed -E \
-e :a \
-e 's/(.*[0-9])([0-9]{3})/\1,\2/' \
-e ta
1,234,567,890
sed -e :a \
-e '/\\$/N' \
-e 's/\\\n//' \
-e ta
sed ':x
/\\$/ {
N
s/\\\n//g
bx
}'
a \
b \
c
d \
e
f
a b c
d e
f
printf "%s\n" {a..h} | \
sed -n '
:a
$! { N; ba }
s/\n/:/gp
'
a:b:c:d:e:f:g:h
#!/bin/sed -f
/<!--/! b
:a
/-->/! {
N
ba
}
s/<!--.*-->//
T1
T2 <!-- C1 --> T3
T4 <!-- C2 C3 --> T5
T6 <!-- C4 --> T7 <!-- C5 --> T8
echo 1 | sed --debug '\%1%s21232'
/1/ s/1/3/
'STDIN' line 1
1
/1/ s/1/3/
3
3
printf '%03d\n' {1..10} | sed --debug '/[02468]$/{N;s/\n/-/}'
=
l
l width
- sets output width
=
l
l width
- nastaví šířku výpisu
man sed | sed -n $=
printf '%s\n' {1..5} | \
sed '
:a
=
$! {
l
N
b a
}
l
s,\n,\t,g
l
'
1
1$
2
1\n2$
3
1\n2\n3$
4
1\n2\n3\n4$
5
1\n2\n3\n4\n5$
1\t2\t3\t4\t5$
1 2 3 4 5
sed -n /ROOT/Ip /etc/passwd
sed s/foo/bar/I <<<FoO
sed -n /ROOT/Ip /etc/passwd
sed s/foo/bar/I <<<FoO
/x/y
sed -En '\_:(/[^:/]+){2}$_p' /etc/passwd
RE1|RE2
)sed -n '/^\(root\|uucp\):/p' /etc/passwd
sed -En '/^(root|uucp):/p' /etc/passwd
man man | sed '/^[[:blank:]]*$/d'
man man | sed '/^\s*$/d'
/x/y
sed -En '\_:(/[^:/]+){2}$_p' /etc/passwd
RE1|RE2
)sed -n '/^\(root\|uucp\):/p' /etc/passwd
sed -En '/^(root|uucp):/p' /etc/passwd
man man | sed '/^[[:blank:]]*$/d'
man man | sed '/^\s*$/d'
FIRST~STEP
seq 10 | sed -n 0~2p # even
seq 10 | sed -n 1~2p # odd
0,/RE/
ADDR1,+N
ADDR1,~N
FIRST~STEP
seq 10 | sed -n 0~2p # even
seq 10 | sed -n 1~2p # odd
0,/RE/
ADDR1,+N
ADDR1,~N
\n
, \r
, \t
\cX
\dXXX
, \oXXX
, \xXX
\n
, \r
, \t
\cX
\dXXX
, \oXXX
, \xXX
echo 𑁍 | sed s/./X/g # Brahmi Punctuation Lotus (U+1104D)
X
echo 𑁍 | LC_ALL=C sed s/./X/g
XXXX
e
e command
e
e command
sed -e 's/^/pwd/;e' -e 's,/,_,g' <<<''
sed -n 'e date' <<<''