Tuesday, September 2, 2014

Unix_SED

SED is a special editor used for modifying files automatically.
1. Write a command to replace the word “bad” with “good” in file?
sed s/bad/good/ < filename
2. Write a command to replace the word “bad” with “good” globally in a file?
sed s/bad/good/g < filename
3. Write a command to replace the character ‘/’ with ‘,’ in a file?
sed ‘s/\//,/’ < filename
sed ‘s|/|,|’ < filename
4. Write a command to replace the word “apple” with “(apple)” in a file?
sed s/apple/(&)/ < filename
5. Write a command to switch the two consecutive words “apple” and “mango” in a file?
sed ‘s/\(apple\) \(mango\)/\2 \1/’ < filename
6. Write a command to replace the second occurrence of the word “bat” with “ball” in a file?
sed ‘s/bat/ball/2′ < filename
7. Write a command to remove all the occurrences of the word “jhon” except the first one in a line with in the entire file?
sed ‘s/jhon//2g’ < filename
8. Write a command to remove the first number on line 5 in file?
sed ’5 s/[0-9][0-9]*//’ < filename
9. Write a command to remove the first number on all lines that start with “@”?
sed ‘\,^@, s/[0-9][0-9]*//’ < filename
10. Write a command to replace the word “gum” with “drum” in the first 100 lines of a file?
sed ’1,00 s/gum/drum/’ < filename
11. write a command to replace the word “lite” with “light” from 100th line to last line in a file?
sed ’100,$ s/lite/light/’ < filename
12. Write a command to remove the first 10 lines from a file?
sed ’1,10 d’ < filename
13. Write a command to duplicate each line in a file?
sed ‘p’ < filename
14. Write a command to duplicate empty lines in a file?
sed ‘/^$/ p’ < filename
15. Write a sed command to print the lines that do not contain the word “run”?
sed -n ‘/run/!p’ < filename

 Options:


Unix-Sed



Sed Commands

= - print line number

a \ - Append
b label - Branch
c \ - change
d and D - Delete
g and G - Get
h and H - Hold
i \ - Insert l - Look
n and N - Next
p and P - Print
q - Quit
r filename - Read File
s/..../..../ - Substitute t label - Test
w filename - Write Filename
x - eXchange
y/..../..../ - Transform

Sed Pattern Flags
/g - Global
/I - Ignore Case
/p - Print
/w filename - Write Filename

Sed Command Line options
-e script (--expression=script)
-f scriptfile (--file=scriptfile)
-h (--help)
-n (--quiet --silent)
-V (--version)

No comments: