CONTENT
  • CHANGES
Szukaj
counter

#top sed


sed - stream editor for filtering and transforming text

Powiązane:
cat, grep, head, sed, sort, tail, tr, uniq, wc,

SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...


DESCRIPTION



OPTIONS
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

-n, --quiet, --silent
suppress automatic printing of pattern space

-e script, --expression=script
add the script to the commands to be executed

-f script-file, --file=script-file
add the contents of script-file to the commands to be executed

-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)

-c, --copy
use copy instead of rename when shuffling files in -i mode (avoids change of input file ownership)

-l N, --line-length=N
specify the desired line-wrap length for the 'l' command

--posix
disable all GNU extensions.

-r, --regexp-extended
use extended regular expressions in the script.

-s, --separate
consider files as separate rather than as a single continuous long stream.


Zobacz także: awk | grep | sed | unbuffer | setbuf() | setbuffer() | setlinebuf() | setvbuf()
-u, --unbuffered
load minimal amounts of data from the input files and flush the output buffers more often

--help
display this help and exit

--version
output version information and exit

If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read.

E-mail bug reports to: bonzini@gnu.org . Be sure to include the word ''sed'' somewhere in the ''Subject:'' field.



EXAMPLES
awk=awk+grep
ps -Ao pid,user,args | grep  'user' | awk '{print($1" "$2);}'
ps -Ao pid,user,args | awk  '/user/{print($1" "$2);}'

sed=sed+grep
ps -Ao pid,user,args | grep  'user' | sed -n 's,^[\t ]*\([^\t ]*[\t ]*[^\t ]*\)[\t ].*$,\1,p'
ps -Ao pid,user,args | sed  -n 's,^[\t ]*\([^\t ]*[\t ]*user*\)[\t ].*$,\1,p'

wybrane parametry:

replaces only 1st instance in a line
sed 's/foo/bar/'

replaces only 4th instance in a line
sed 's/foo/bar/4'

replaces ALL instances in a line
zamiana wyrażenia source na tekst destination we wszystkich liniach
sed 's/foo/bar/g'
sed 's/source/destination/g'

zamiana wyrażenia source na tekst destination w linii 47
sed '47s/source/destination/g'

zamiana wyrażenia source na tekst destination w liniach od 10 do 20
sed '10,20s/source/destination/g'

replace only first occurrence (first instance), zamiana pierwszego znalezionego wyrażenia source (w dowolnej linni) na destination,
(UWAGA! wymaga użycia znaków "/")
sed '0,/source/s//destination/'



wyświetlenie linii pasujących do regexp,
(UWAGA! wymaga użycia znaków "/")
sed -n '/regexp/p'

wyświetlenie wartości pasującej do klucza keyname (usuwa wyrażenie "keyname =" i wyświetla pozostałe wyrażenie, ~ wartość),(
UWAGA! wymaga użycia znaków "/")
sed -n '/^keyname = //p'

wyświetlenie wartości (bez komentarza) pasującej do klucza keyname (usuwa wyrażenie "keyname =" i wyświetla pozostałe wyrażenie, ~ wartość),
(UWAGA! wymaga użycia znaków "/")
sed -n '/^keyname = \([^#]*\)#.*$/\1/p'

zwraca teskt pomiędzy podanymi wyrazami teskt1 i teskt2, brak opcji -n odwraca sposób działania polecenia,
(UWAGA! wymaga użycia znaków "/")
sed -n '/tekst1/,/teskt2/p'



add line at the beggining of file
sed '1s/^/<?php\n/'

add line to the end of file
sed '$s/$/\n?>/'



usunięcie linii pasującej do regexp,
(UWAGA! wymaga użycia znaków "/")
sed '/regexp/d'

usunięcie pierwszej linii
sed '1d'

usunięcie wszystkich linii oprócz lini 5
sed '5d'
echo -en "111\n222\n333\n444\n555\n" | sed '4,$d' # result: echo -en "555\n"

usunięcie wszystkich linii począwszy od lini 2 do linii 4
sed '2,4d'
echo -en "111\n222\n333\n444\n555\n" | sed '4,$d' # result: echo -en "111\n555\n"

usunięcie ostatniej linii
sed '$d'

usuń wszystkie linie oprócz 1 (wyświetl 1 linie pliku, analogiczne do head -1)
sed '1!d'

usuń wszystkie linie oprócz 47 (wyświetl 47 linie pliku)
sed '47!d'

usuń wszystkie linie oprócz przedziału 47-108 (wyświetl linie z przedziału 47 do 108)
sed '47,108!d'

usuń wszystkie linie oprócz ostatniej (wyświetl ostatnią linie pliku, analogiczne do tail -1)
sed '$!d'



dwie alternatywne składnie
sed -e 's/\\\x95/\|/g'
sed -e 's/\x95/|/g'



evaluate date:
echo 1234567890 | sed -n 's/^\([0-9]*\)$/echo "$(date -d @\1)"/ep'
Rezultat:
Sat Feb 14 00:31:30 CET 2009

The upper and lowercase letters are handled by :
\E No change
\U Makes all text to the right uppercase.
\u makes only the first character to the right uppercase. (Note its a lowercase letter U)
\L Makes all text to the right lowercase.
\l Makes only the first character to the right lower case. (Note its a lowercase letter L)
replace patern:
echo "Big Letters = Lower Letters" | sed 's/^\([A-Za-z0-9 ]*\)=\([A-Za-z0-9 ]*\)$/\U\1=\L\2/g'
Rezultat:
BIG LETTERS = lower letters



three alternative syntax:
sed '/^[0-9]*[acd][0-9]*/d;s/^[<>] //g'
sed -e '/^[0-9]*[acd][0-9]*/d' -e 's/^[<>] //g'
sed '/^[0-9]*[acd][0-9]*/d' | sed  's/^[<>] //g'




Zmodyfikowany ostatnio: 2015/10/22 01:01:56 (8 lat temu), textsize: 7,56 kB, htmlsize: 12,1 kB

Zapraszam do komentowania, zgłaszania sugestii, propozycji, własnych przykładów, ...
Dodaj komentarzKomentarze użytkowników