Professional Documents
Culture Documents
Sed Filter in Unix Complete
Sed Filter in Unix Complete
To edit files too large for comfortable interactive editing; To perform multiple `global' editing functions efficiently in one pass through the input.
Takes input from a file, but its output is sent to std.out The original file is not altered Output from sed can be redirected to a new file if you want to save the changes or piped to another program (Vi , ed , ex editors create a temporary copy of the file you want to edit in /tmp. All changes are made to this copy. When you save these changes (:w) , the temporary file is copied to the original)
substitutecommand. resulting line match in thezero pattern space: Set ofThe Lines - a RE that can or more lines, not necessarily consecutive (/^A/ command) amyenjoyshikingandbenenjoyshiking
Ranges an address range that defines a set of consecutive lines (lineno,lineno ; /RE/,lineno) { /never/ command } )
2) Then the second substitute command is executed on the line in the pattern space, and the result isAddresses : Nested an address contained within another address (20,30 amyenjoysbikingandbenenjoysbiking 3) And the result is written to standard out.
sedediting-commandfilename
Line specifier editing instruction
3 Third line 5,8 lines 5 to 8 10,$ lines 10 to last line $-9,$ sed cannot process this /huge/ all lines containing huge /^[ \t]*$/ all blank lines 1,/gossip/ from line 1 to first mention of gossip <specifier>! Command applied to lines that do not match specifier
s (substitute)
p (print)
d (delete)
q (quit)
sed10qtextquitafterreadingline10 sed/^[\t]*$/dtextdeleteallblanklines sed/value/!dportfoliodeleteeverylineinportfolionotcontainingthestringvalue sed n9,11pemplist -n suppresses duplicates sed n/obr[iy][ae]n/p /lennon/pemplist eitherobriensorlennons
(note and separate lines)
s/oldstring/newstring/
seds/Michael/Michele/text sed1,/Jessup/s/Michael/Michele/text seds/UNIX/UNIX/adcopy seds/UNIX/&/adcopy seds/\(incredible\) and \(amazing\)/ \2 or \1/paper
tagged item #1 tagged item #2
simple strings, can contain special chars, tag part of the pattern, all of grep expressions ,&
* Replace all occurrences of foregoon and Foregone with foregone and Foregone
Sed..substitute
[yma@yma ~]$ cat sedtext this line has o'brien and this has a lennon this has neither and this has both o'bryan and lennon [yma@yma ~]$ sed 's/"o'brien"/oooooo/g' sedtext > bash: unexpected EOF while looking for matching `"' bash: syntax error: unexpected end of file [yma@yma ~]$ sed 's/"o\'brien"/oooooo/g' sedtext > bash: unexpected EOF while looking for matching `"' bash: syntax error: unexpected end of file yma@yma]~$seds/o'brien/oooooo/gsedtext this line has oooooo and this has a lennon this has neither and this has both o'bryan and lennon
ls l | sed n/^..w/p
lists files which have write permissions for the group
Sed ..examples
[root@localhost ~]# cat therav Once upon a midnight dreary,while I pondered weak and weary. Over many a quaint and curious volume of forgotten lore While i nodded,nearly napping,suddenly there came a tapping display line numbers [root@localhost ~]# sed "=" therav 1 Once upon a midnight dreary,while I pondered weak and weary. 2 Over many a quaint and curious volume of forgotten lore 3 While i nodded,nearly napping,suddenly there came a tapping [root@localhost ~]#
Sed ..examples
Print lines beginning with O [root@localhost ~]# sed -n /^O/ therav sed: -e expression #1, char 4: missing command [root@localhost ~]# [root@localhost ~]# sed /^O/p therav Once upon a midnight dreary,while I pondered weak and weary. Once upon a midnight dreary,while I pondered weak and weary. Over many a quaint and curious volume of forgotten lore Over many a quaint and curious volume of forgotten lore While i nodded,nearly napping,suddenly there came a tapping [root@localhost ~]# [root@localhost ~]# sed -n /^O/p therav Once upon a midnight dreary,while I pondered weak and weary.
Sed ..examples
Using substitute command [root@localhost ~]# echo 123abc456|sed 's/[0-9]//'
23abc456
If global substitution not specified, sed commands operate on only the first occurrence of the pattern
Sed ..examples
adds spaces at beginning of line and -- to the end Script stored in a file called addpart.sed [root@localhost ~]# cat >addpart.sed s/^/ / s/$/--/ [root@localhost ~]# Invoking the script in sed [root@localhost ~]# sed -f addpart.sed therav Once upon a midnight dreary,while I pondered weak and weary.-Over many a quaint and curious volume of forgotten lore-While i nodded,nearly napping,suddenly there came a tapping-[root@localhost ~]#
Sed ..examples
Using the Back Reference (Whole pattern substitution) Once upon a midnight dreary,while I pondered weak and weary. Over many a quaint and curious volume of forgotten lore While i nodded,nearly napping,suddenly there came a tapping [root@localhost ~]# sed 's/on/&once/' therav Once upononce a midnight dreary,while I pondered weak and weary. Over many a quaint and curious volume of forgotten lore While i nodded,nearly napping,suddenly there came a tapping [root@localhost ~]# sed 's/o*e/&once/g' therav Onceonce upon a midnight dreonceary,whileonce I pondeoncereonced weonceak and weonceary. Oveoncer many a quaint and curious volumeonce of forgotteoncen loreonce Whileonce i noddeonced,neoncearly napping,suddeoncenly theoncereonce cameonce a tapping [root@localhost ~]#
10
sed ...contd
Reading a file ( r ) the r command lets you read in a file at a specified location. $sed/<FORM>/rtemplate.htmlform_entry.html
inserts the forms details from an external file template.html after the <FORM> tag
$sed '/Over/r addpart.sed' therav Inserting and changing text ( i , a , c) for appending text use a command and then enter as many lines as you want. you have to precede the [ENTER] key in each line except the last with a \.
$sed$a\ > place the following line at the end \ > The end ; > www_lib.pl > $$
$sedi\ fil1
Once upon a midnight dreary, while I pondered , weak and weary Over many a quaint and curious volume of forgotten lore While I nodded, nearly napping, suddenly there came a tapping
$sed2c\ Over many an obscure and meaningless problem of calculus bore fil1 11
sed..examples
Add a title to the file therav [root@localhost ~]# cat > insfil.sed #script to add a title to file 1i\ The Raven\ by Edgar Poe\ [root@localhost ~]# sed -f insfil.sed therav sed:fileinsfil.sedline5:unknowncommand:`E [root@localhost ~]# vi insfil.sed
12
sed...examples
[root@localhost ~]# cat insfil.sed
#script to add a title to file 1i\ The Raven\ by\ Edgar Poe\
The Raven by Edgar Poe Once upon a midnight dreary,while I pondered weak and weary. Over many a quaint and curious volume of forgotten lore While i nodded,nearly napping,suddenly there came a tapping [root@localhost ~]#
Once upon a midnight dreary,while I pondered weak and weary. Over many a quaint and curious volume of forgotten lore While i nodded,nearly napping,suddenly there came a tapping
The Awful Truth about sed ---sed has a serious learning curve!!
Write a command in sed that adds == at the beginning of a line, !! at the end of the line; and a blank line between every line
sed...examples
Once upon a midnight dreary,while I pondered weak and weary. Over many a quaint and curious volume of forgotten lore > Tis a visitor\ a*b' While i nodded,nearly napping,suddenly there came a tapping Matches zero or more `a's followed by a single `b'. For example, `b' or `aaaaab > at the door ' therav >newfil Tis a visitor [root@localhost ~]# cat newfil at the door a\+b\+' Matches one or more `a's followed by one or more `b's: `ab' is the shortest possible match. .*' .\+' These two both match all the characters in a string; however, the first matches every string (including the empty string), while the second matches only strings containing at least one character.
^main.*(.*)' This matches a string starting with `main', followed by an opening and closing parenthesis. The `n', `(' and `)' need not be adjacent.
`^#' This matches a string beginning with `#'. `\\$' This matches a string ending with a single backslash. The regexp contains two backslashes for escaping.
`\$'
Instead, this matches a string consisting of a single dollar sign, because it is escaped.
14
Sed..expressions
[yma@yma
~]$ cat ab
ab abc aaab abab ababc aabababddc ab.abc a?b b abc\ ab$ $ / \ [yma@yma ~]$ sed -n '/\/$/p' ab / [yma@yma ~]$ sed -n '/\\$/p' ab abc\ \ [yma@yma ~]$ sed -n '/\$/p' ab ab$ $ $[yma@yma~]
sed...examples
[yma@pc18 ~]$ cat mydat this is a test file for removing the rest of the words in a file but keeping only the first word
[yma@pc18 ~]$ sed 's/\(on\)/\1eeee/' mydat
this is a test file for removing the rest of the words in a file but keeping only the first word
this is a test file for removing the rest of the words in a file but keeping oneeeely the first word
Sowhydidntseddoanything?
16
sed...examples
[yma@pc18 ~]$ sed 's/\(the\) rest \(of\)/\2 and \1/' mydat this is a test file for removing
of and the
the words int a file but keeping only
sed...contd
Substitution : no specification --> done for first occurrence g --> done globally
[yma@pc18 ~]$ cat mydat this is a test file for removing the rest of the words in a file but keeping only the first word
Write a sed command To keep the first word on the line but delete the second
If you want to specify which pattern to match use /1 /2 ... (Do Not confuse this with \1 \2 ...)
18
sed...examples
To add a character (!) after the 10th char on a line [yma@pc18 ~]$ vi mypass
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin torrent:x:496:490:BitTorrent Seed/Tracker:/var/spool/bittorrent:/sbin/nologin backuppc:x:495:489::/var/lib/BackupPC:/usr/bin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin gdm:x:42:42::/var/gdm:/sbin/nologin student:x:500:500:student:/home/student:/bin/bash yma:x:501:501::/home/yma:/bin/bash std1:x:502:502::/home/std1:/bin/bash
[yma@pc18 ~]$ sed 's/./&!/10' mypass nfsnobody:!x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin torrent:x:!496:490:BitTorrent Seed/Tracker:/var/spool/bittorrent:/sbin/nologin backuppc:x!:495:489::/var/lib/BackupPC:/usr/bin/nologin haldaemon:!x:68:68:HAL daemon:/:/sbin/nologin gdm:x:42:4!2::/var/gdm:/sbin/nologin student:x:!500:500:student:/home/student:/bin/bash yma:x:501:!501::/home/yma:/bin/bash std1:x:502!:502::/home/std1:/bin/bash [yma@pc18 ~]$
20
sed...contd
Sending output to a file :
[yma@pc18 ~]$ sed -n 's/./&!/10w mypass.out' mypass [yma@pc18 ~]$ cat mypass.out
nfsnobody:!x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin torrent:x:!496:490:BitTorrent Seed/Tracker:/var/spool/bittorrent:/sbin/nologin backuppc:x!:495:489::/var/lib/BackupPC:/usr/bin/nologin haldaemon:!x:68:68:HAL daemon:/:/sbin/nologin gdm:x:42:4!2::/var/gdm:/sbin/nologin student:x:!500:500:student:/home/student:/bin/bash yma:x:501:!501::/home/yma:/bin/bash std1:x:502!:502::/home/std1:/bin/bash
Modify the script to accept an argument from the command line this is a test file for removing the rest of the words in a file but keeping only the first word
[yma@pc18 ~]$ vi sedscrp.sed sed 's/$1/replaces/g' mydat [yma@pc18 ~]$ ./sedscrp.sed removing SoWhydidntanythinghappen?
21
sed...examples
[yma@pc18 ~]$ vi sedscrp.sed sed 's/'$1'/replaces/g' mydat [yma@pc18 ~]$ ./sedscrp.sed removing
this is a test file for replaces the rest of the words in a file but keeping only the first word
[vi sedc1]
sed1,/stop/ s/#.*//'
Tr : character transliteration
Tr replaces characters in one list with corresponding characters in another list Tr is a pure filter (use redirection to access a file)
(file) Philosophy : I compute, therefore $tr eiou ~#$% < philosophy I c$mp%t~ , th~r~f$r~ I am case sensitive
I am
$tr[A-Z][a-z] $tr\012#<philosophy
I compute ,therefore I am # $
ranges accepted
\012 is ASCII representation of newline
23
Tr..contd
Options
-d (causes list of characters to be deleted) -c (causes substitution to affect every char but those in the first list) -s (compresses strings of repeated o/p chars into a single char) $tr -d eoiu <philosophy $treiou<*philosophy to protect from shell $tr ceiou<*philosophy $tr cseiou<*philosophy I cmpt thrfr I am I c*mp*t* , th*r*f*r* I am ***o**u*e****e*e.. *o*u*e*e*e.
1st string consists of all alphabetic characters -c says substitution affects all chars accept those alphabetic characters All affected characters to be replaced by newline -s says all multiple newlines to be replaced by single newline
24