-
Notifications
You must be signed in to change notification settings - Fork 0
/
sed.sh
127 lines (85 loc) · 3.57 KB
/
sed.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
filepath='sampleTexts/sed.txt'
echo -e "\n\n---- sed command operates line by line on a file ----"
echo -e "\n\n---- Replacing the first occurence of a string using 's' ----"
sed 's/unix/linux/' $filepath
echo -e "\n\n---- Replacing the nth occurence of a string using 's/../n' n=2 ----"
sed 's/unix/linux/2' $filepath
echo -e "\n\n---- Replacing all occurences of a string using 's/../g' ----"
sed 's/unix/linux/g' $filepath
echo -e "\n\n---- Replacing all occurence from the nth occurence of a string using 's/../ng' ----"
sed "s/unix/linux/2g" $filepath
echo -e "\n---- Using & to denote the matched string ----"
sed 's/unix/{&}/g' $filepath
echo
sed 's/unix/&&/g' $filepath
echo -e "\n\n---- Use () to remember the matched string ----"
echo "---- We swapped the matched words ----"
sed 's/\(unix\)\(linux\)/\2\1/g' $filepath
echo -e "\n\n---- Replacing a string on specific line number ----"
sed '1 s/unix/linux/g' $filepath
echo -e "\n\n---- Replacing a string on a range of lines ----"
sed '1,3 s/unix/linux/g' $filepath
echo -e "\n----Multiple sed commands using -e ----"
sed -e 's/unix/linux/' -e '1 s/os/system/g' $filepath
echo -e "\n\n---- sed can work as grep as well as tr command ----"
echo -e "\n----sed working as grep command ----"
sed -n '/unix/ p' $filepath
echo -e "\n----sed working as grep -v command ----"
sed -n '/unix/ !p' $filepath
echo -e "\n----sed working as tr command ----"
sed 'y/sy/mi/' $filepath
: '
Output:
---- sed command operates line by line on a file ----
---- Replacing the first occurence of a string using 's' ----
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
---- Replacing the nth occurence of a string using 's/../n' n=2 ----
unix is great os. linux is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
---- Replacing all occurences of a string using 's/../g' ----
linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.
---- Replacing all occurence from the nth occurence of a string using 's/../ng' ----
unix is great os. linux is opensource. linux is free os.
learn operating system.
unixlinux which one you choose.
---- Using & to denote the matched string ----
{unix} is great os. {unix} is opensource. {unix} is free os.
learn operating system.
{unix}linux which one you choose.
unixunix is great os. unixunix is opensource. unixunix is free os.
learn operating system.
unixunixlinux which one you choose.
---- Use () to remember the matched string ----
---- We swapped the matched words ----
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxunix which one you choose.
---- Replacing a string on specific line number ----
linux is great os. linux is opensource. linux is free os.
learn operating system.
unixlinux which one you choose.
---- Replacing a string on a range of lines ----
linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.
----Multiple sed commands using -e ----
linux is great system. unix is opensource. unix is free system.
learn operating system.
linuxlinux which one you choose.
---- sed can work as grep as well as tr command ----
----sed working as grep command ----
unix is great os. unix is opensource. unix is free os.
unixlinux which one you choose.
----sed working as grep -v command ----
learn operating system.
----sed working as tr command ----
unix im great om. unix im openmource. unix im free om.
learn operating mimtem.
unixlinux which one iou choome.
'