Sie sind auf Seite 1von 7

UNIX Sed Command Options

1).sed -n '$=' myfile.txt


Above this command count the number of lines in the myfile.txt and output the results.
2).Wite fist ! last line of t"e file
In this example, 1 and $ refers first and last line respectively.
$ sed -n -e '1w output.txt' -e '$w output.txt' >theee!stuff.txt
#).Wite t"e lines mat$"es %it" t"e patten Stoa&e o Sysadmin
In this example sed command writes the lines which matches the pattern "#torae$ or
"#ysadmin$.
$ sed -n -e '%#torae%w output.txt' -e '%#ysadmin%w output.txt' theee!stuff.txt
').Wite t"e lines fom %"i$" t"e patten mat$"es to till end of t"e file
In this example, %#torae%,$ represents line matches from #torae to end of the file.
$ sed -n '%#torae%,$w output.txt' theee!stuff.txt
().Wite t"e lines %"i$" mat$"es patten and next t%o lines fom mat$"
In this example, the send command writes the line matches for "#torae$ and two lines next
to that.
$ sed -n '%#torae%,&'w output.txt' theee!stuff.txt
)). Wite 1st line of t"e file
In this example, 1 (address) refers the first line of the input and
w writes the pattern buffer to the output file "output.txt$
$ sed -n '1w output.txt' theee!stuff.txt
*o% to +isplay 1st and #
d
line in a ,ile
#ed *n +1p,-p. /file name>
-).+elete 't" and 2nd line fom t"e inp.t
0his sed example deletes 1th and 'nd line from the file theee!stuff.txt. 2sin "-e$ option,
you can ive any number of commands with sed.
$ sed -e '1d' -e ''d' theee!stuff.txt
/). +elete t"e fist0 last and all t"e 1lan2 lines fom inp.t
0his sed example deletes the first line, last line and all the blan! lines from input file.And
redirect to file 3ohnny.txt
$ sed -e '1d' -e '$d' -e '%4$%d' theee!stuff.txt >3ohnny.txt
3).sed -e 's4lin.x4.nix4' -e 's4afte41efoe4'
15).6o emo7e all of t"e lines $ontainin& 8t%o8 fom t"e myfile.txt file9
$ sed '%two% d' myfile.txt
11).6o emo7e t"e fist t"ee lines fom t"e display0 e&adless of %"at t"ey ae9
$ sed '1,- d' myfile.txt
12).6o delete all lines t"at $ontain a 'Windo%s' %od0 ente9
$ sed '%5indows%d' %tmp%data.txt > %tmp%output.data.txt
1#).6o delete t"e lines 2 t"o.&" ' in t"e a1o7e file0 .se t"e follo%in& sed
$ommand
$ sed '',1d' textfile.txt
1').+elete all o$$.en$es of a patten in a file .sin& sed $ommand
0his is a simple application of the %s fla where the taret strin is none.
6or example, to remove all occurrences of word 7line7 in the above file, the sed command
would be
$ sed 's%line%%' textfile.txt
0his is 10
his is '
0his is -
0his is 1
0his is 8
18).0o remove all occurrences of the character space in the above input file,
the sed command would be
bash--.99: sed 's% %%' textfile.txt
0hisisline1
0hisisline'
0hisisline-
0hisisline1
0hisisline8
1)).+eletin& lines %"i$" mat$"es a patten .sin& sed $ommand
0o delete lines which matches a pattern, use the %d fla alon with the
pattern to be matched, for example, to delete lines matchin the pattern 7line 17 in the
above file, the command would be
sed '%line 1%d' textfile.txt
1-).+eletin& t"e fist line %"i$" mat$"es a patten .sin& sed $ommand
sed '%line%;1d,<' textfile.txt
1/).+eletin& t"e last line %"i$" mat$"es a patten .sin& sed $ommand
sed '%line%;$d,<' textfile.txt
6o sea$" and epla$e0 .se t"e sed 's' a$tion0 %"i$" $omes in font of t%o
expessions9
cat filename | sed 's/string_old/string_new/' > newfile
S.1stit.te Only 2nd O$$.en$e of a Wod Usin& sed s442
$ sed 's%=inux%=inux-2nix%'' theee!stuff.txt
s%>>%->>>>?.99% search for all '>>' replace with '->>>>.99'
s%@asinA9->B%% remove the word @asin followed by a sinle diit
s%@asin$%% remove the word @asin if it is at the end of
the line.
sed commands are usually on one line
if we want more (multi-line commands), then we must end the first line with an C?'
if a command is one line only, it can be separated by a C,+
if it is a multi-line, then it must contain all of its line (except the first) by themselves
on command line, what follows a C-e' is li!e a whole line in a sed script
%..D% At least 1 character lon (%.&% means the same thin)
%4$% the empty line
abEcd Fither +ab. or +cd.
append
$ $"an&e lines
d delete lines
i inset
p pint lines
s s.1stit.te
n display lines
: ;.it lines
Gelete commentsH sed -e '%4:%d+
<emo7e all empty lines9
sed '%4$%d' filename.txt
sed '%.%Id' filename.txt
sed -i7 overwrites the oriinal file with a new one
*o% to =int only >lan2 ?ine of ,ile.
sed -n '%4$%p' /6ile Jame>
Command to +elete Commented ?ines and >lan2 ?ies
sed -e 's%:.D%%' -e '%4$% d'
6o delete only t"e %od .se9
sed 's%yourword%%' yourfile
0o delete two words from a file simultaneously useH
sed -e 's%firstword%%' -e 's%secondword%%' yourfile
Kipeline
Kipeline lets prorams use the output of a proram as the input of another one with the
symbolH E
0a!e the previous exampleH
cat file1 E sed s%Lello%5ord% > file'
myproram.exe file' > file-
6o =int ,ist and ?ast ?ine .sin& Sed Command
#ed -n +1. p /6ileMJame>
#ed *n +$. p /6ileMname>
6o =int all line @x$ept ,ist ?ine
#ed *n +1I. p /6ileMJame>
+elete all ?ine ex$ept ,ist ?ine
#ed *n +1I. d /fileMname>
+elete all ?ine %"i$" mat$"es a %od
sed *i +%a%d. /filename>
Command to find #
d
Col.mn0 't" <o% in t"at ,ist '$"as9
Aw! *6 +$. +;print $-<.Eaw! +JN>1.Esed *n +1p.Ecut *c 1-1
Ai @dito B,ind and <epla$e)
HAraneBsAubstituteB%;pattern<%;strin<%AflasB AcountB
H1,19s%hello%Lello%
+elete Statin& fom #d line and e7ey 2nd line fom t"ee.
#ed +-O'd. /6ileJame>
Lere in this command it starts deletin from -
rd
line from then every '
nd
line till end of the
file.
+eletin& fom 't" to /t" line in a file.
#ed +1,Pd. /6ileJame>
+elete line %"i$" Cat$"ed patten.
#ed +%done%d. /filename>
#ed +%done%,$d. /6ileJame> here it will delete wherever done matches till last line.
#ed +%done%,&'d. /6ileJame> here it will delete wherever done matches from there next
'lines.
+elete lines ot"e t"an t"e spe$ified an&e
#ed +',1Id. /6ileJame>
+elete fist and last line
#ed +1d,$d. /6ileJame>
+elete lines t"at 1e&in %it" pati$.la $"aa$te
#ed +%$a%d. /6ileJame>
#ed +%4a%d. /6ileJame> Geletes line which starts with letter +a..
#ed +%$a%d. /6ileJame> Geletes line which ends with letter +a..
Undo and C"an&e Commands Command Ceanin&
$% Qhane word (or part of word) at the cursor location to the
end of the word
#$% Qhane three words
Neplace character at cursor with one other character
. 2ndo previous command
>asi$ Sea$" Commands Command Ceanin&
D B.ppe $ase) Ro to last line of file
921 Ro to line '1
4stin& #earch forward for strin
Estin& #earch bac!ward for strin
n 6ind next occurrence of strin
Split Command in ?in.x
-l SptionH
2sin this command we can split the records into multiple number of files.
#plit the file into multiple pieces based up on the number of lines usin -l option as shown
below.
SyntaxH split *l(lines) /rooflines> /Actual 6ilenames> /After#plt6ileJame>
-d Option 9
2se -d option to name the files with number suffixes as 99, 91, 9' .. and so on, instead of
aa, ab, ac.
Syntax H $ split -d testfile
$ ls -ltr
testfile x99 x91 x9'
*o% to ,ind t"e len&t" of a ,ile Name
ATCexpr lenth /6ileJame>
Fcho $A
*o% to find len&t" of ea$" line in a file
Aw! +;print lenth<. /6ileJame>
*o% to set n.m1es in Ai @dito
Hset nu Sr Hset number
*o% to emo7e line n.m1es in Ai @dito
Hset nuI Sr Hset nonu Sr Hset nonumber
*o% to &et =e7io.s date in Unix
Gate &UV-Um-Ud
date &UV-Um-Ud *date "- 1 day$
date &UV-Um-Ud --date 7- 1 month7
date &UV-Um-Ud --date 7- 1 year7
Common ,.n$tionalities of S@+ and FWG Command 9
1 ). 5e can 6ind 0otal no of record count
#ed H sed -n '$T' /6ile Jame>
Aw! H cat ArchiveM#cript.#LEaw! ';print JN<'Etail -1
'). 5e can et exact Necords in a file
#ed H sed *n +8p. /6ile Jame>
Aw! H cat ArchiveM#cript.#LEaw! 'JNTT8.
*o% to &et only Heo >yte files %"i$" ae pesent in t"e die$toy
F%2 9
=s *ltr E aw! +%4-% ; if($8 TT9) print $> <.
,ind 9
find -type f -siWe 9
*o% to ,ind t"e len&t" of ea$" file in a die$toy9
ls E aw! '$(J6&1)Tlenth'
*o% add a ,ist e$od and ?ast <e$od to t"e $.ent file in ?in.x
sed -i -e '1i Leader' -e '$a 0railor' #uccess.txt
anot"e %ay
sed -i -e '1i Leader' #uccess.txt
Fcho 0railor>>#uccess.txt(It will append at end of a line).
*o% to display @7en n.m1e of e$ods into one file and Odd n.m1e of e$ods
into anot"e file
Aw! +JN U ' TT 9. /6ile Jame > >Fven
Aw! +JN U ' TT 9. /6ile Jame > >Sdd
At end records will load into even and odd files.
F%2 Stin& ,.n$tions9
=enthH echo 73ana!iram Gabbara7Eaw! ';print lenth<'
#ubstrH echo 73ana!iram Gabbara7Eaw! ';print #ubstr ($9,1,>)<'
0oupperH echo 73ana!iram Gabbara7Eaw! ';print toupper ($9)<'
0olower H echo 73ana!iram Gabbara7Eaw! ';print tolower($9)<'
6o see last exe$.ted $ommands in ?in.x9
#ed I#ed
5c I5c
Rrep Irep
Aw! Iaw!
6ind Ifind
Lead ILead and so on for some more commands.

Das könnte Ihnen auch gefallen