Академический Документы
Профессиональный Документы
Культура Документы
Contents
To execute sed from file
Sed regular expression
Contents
To execute sed from file
Sed regular expression
8.
line 1 (one)
line 2 (two)
line 3 (three)
Command:
sed -e '1,2s/line/LINE/' file
Output:
LINE 1 (one)
LINE 2 (two)
line 3 (three)
9. Command:
sed -e '1,2d' file
Output:
line 3 (three)
10. Command:
sed -e '3d' file
Output:
line 1 (one)
line 2 (two)
Usage
\*
\+
\?
[ ]
[^ ]
(character)\{m,n\}
(character)\{m,\}
(character)\{,n\}
(character)\{n\}
\(expression\)
\n
&
Example:
/[Uu]nix/!d
word unix.
6d
deletes line 6
/^$/d
1,10d
`[a-zA-Z0-9]'
This matches any letters or digits.
`[^a-z A-Z] '
This matches any letters .
Repetition using *
means 0 or more of the previous single character
pattern.
[abc]*
Hi Dave.*
0*10
Repetition using +
+ means 1 or more of the previous single character
pattern.
[abc]+
Hi Dave.+
0+10
matches "010" or "0000010" does not
match "10"
a\+b\+
matches one or more `a's followed
by one or more
`b's: `ab' is the shorter
possible match, but other
examples are
`aaaab' or `abbbbb' or
? Repetition Operator
? means 0 or 1 of the previous single character
pattern.
x[abc]?x
`.\{9\}A$'
This matches an A that is the last character on line, with at
least nine preceding characters.
`^.\{15\}A
This matches an A that is the 16th character on a line.
Regular Expressions
(cont)
/^M.*/
/..*/
/^$/
ab|cd
Either ab or cd
a(b*|c*)d
[[:space:]
[:alnum:]
]
Note:
Sed always tries to find the longest matching pattern in the
input. How would you match a tag in an HTML document?
/p' u3
6. Output the file after changing mcis to MCIS on
lines 10-20
Answer:
sed '10,20s/mcis/MCIS/g' u3
sed '1,20d' u3
9.
line 1 (one)
line 2 (two)
line 3 (three)
Command:
sed -e '1,2s/line/LINE/' file
Output:
LINE 1 (one)
LINE 2 (two)
line 3 (three)
9. Command:
sed -e '1,2d' file
Output:
line 3 (three)
10. Command:
sed -e '3d' file
Output:
line 1 (one)
line 2 (two)
11. Write a sed script that will take two words and a file
name a input from the user.
Let the inputs be word1, word2, and filename. Write scripts to do the following
To insert the word2 at every place word1 is present in
the file u3
Answer:
#!/bin/sh
echo -n 'Enter the string to which the new string to be
appended:'
read string1
echo -n 'Enter the string which is used to append:'
read string2
echo -n 'Enter the filename '
read filename
Usage
\*
\+
\?
[ ]
[^ ]
(character)\{m,n\}
(character)\{m,\}
(character)\{,n\}
(character)\{n\}
\(expression\)
\n
Examples:
1,10d
/[Uu]nix/!d
6d
/^$/d
1,/^$/d
deletes line 6
deletes all blank lines
deletes from line 1 through the first
blank line
deletes from the first blank line
through the last line of the file
deletes from the first blank line
through line 10
/^$/,/$/d
/^$/,10d
`[a-zA-Z0-9]'
This matches any letters or digits.
`[^a-z A-Z] '
This matches any letters .
sed '/^[^aeEI]/d'
list.txt
Repetition using *
means 0 or more of the previous single character
pattern.
[abc]*
Hi Dave.*
0*10
s/00*/0/g
Repetition using +
+ means 1 or more of the previous single character
pattern.
[abc]+
Hi Dave.+
0+10
matches "010" or "0000010" does not
match "10"
a\+b\+
matches one or more `a's followed
by one or more
`b's: `ab' is the shorter
possible match, but other
examples are
`aaaab' or `abbbbb' or
? Repetition Operator
? means 0 or 1 of the previous single character
pattern.
x[abc]?x
`a\?b'
Matches `b' or `ab'.
`\{I\}'
As `*', but matches exactly I sequences (I is a decimal
integer;
for portability, keep it between 0 and 255 inclusive).
`\{I,J\}'
Matches between I and J, inclusive, sequences.
`\{I,\}'
Matches more than or equal to I sequences.
`.\{9\}A$
This matches nine characters followed by an `A'.
`^.\{15\}A'
This matches the start of a string that contains
16characters,
the last of which is an `A'.
`\(REGEXP\)
Groups the inner REGEXP as a whole, this is
used to:
* Apply postfix operators, like `\(abcd\)*':
this will search
for zero or more whole sequences of `abcd',
while `abcd*'
would search for `abc' followed by zero or
more occurrences
of `d'. Note that support for `\(abcd\)*' is
required by
POSIX 1003.1-2001, but many non-GNU
implementations do not
support it and hence it is not universally
portable.
`REGEXP1\|REGEXP2'
Matches either REGEXP1 or REGEXP2.
Use parentheses to use complex alternative regular
expressions.
The matching process tries each alternative in turn,
from left to right, and the first one that succeeds is
used.
`N
Add a newline to the pattern space, then append
the next line of input to the pattern space.
If there is no more input then SED exits without
processing any more commands.
File spacing:
space a file
sed G file name
insert a blank line below every line which matches "regex
sed '/regex/G'