Skip to content

Commit a6e1772

Browse files
committed
sed equivalent: till step 5
1 parent ed963d1 commit a6e1772

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### Set alias
2+
3+
$ alias ccsed="python ccsed.py"
4+
5+
### Step: 1
6+
<br>########## ================== <br>
7+
REPLACE QUOTES(") FROM ENTIRE FILE
8+
9+
$ ccsed s/\"//g test.txt
10+
<br>########## ================== <br>
11+
### Step: 2
12+
mimic sed -n '2,4p' file name
13+
14+
$ ccsed -n '2,4p' unquoted.txt
15+
<br>########## ================== <br>
16+
### Step: 3
17+
ccsed -n /pattern/p filename
18+
19+
$ ccsed -n /roads/p unquoted.txt
20+
<br>########## ================== <br>
21+
22+
### Step: 4
23+
to support double spacing a file
24+
25+
$ ccsed G unquoted.txt
26+
<br>########## ================== <br>
27+
### Step: 5
28+
to support deletion
29+
30+
$ ccsed /^$/d unquoted.txt
31+
<br>########## ================== <br>
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import re
2+
import sys
3+
4+
def substitution(file_name, option, what_to_sub, what_to_sub_with, operate_on = "g"):
5+
print(f"file_name={file_name}, option={option}, what_to_sub={what_to_sub}, what_to_sub_with={what_to_sub_with}, operate_on={operate_on}")
6+
with open(file_name, "r+") as f:
7+
file = f.read()
8+
9+
file_modified = re.sub(what_to_sub, what_to_sub_with, file)
10+
f.seek(0, 0)
11+
f.write(file_modified)
12+
f.truncate()
13+
14+
def sedprint(option, start_from, end_to, file_name):
15+
print(f"option={option}, start_from={start_from}, end_to={end_to}, file_name={file_name}")
16+
file = open(file_name, "r")
17+
file_content = file.readlines()
18+
for i in range(int(start_from)-1, int(end_to)):
19+
print(file_content[i].strip())
20+
21+
file.close()
22+
23+
def sedpatternprint(option, string_to_search, what_to_do, file_name):
24+
print(f"option={option}, string_to_search={string_to_search}, what_to_do={what_to_do}, file_name={file_name}")
25+
file = open(file_name, "r")
26+
file_content = file.readlines()
27+
for line in file_content:
28+
#print(line)
29+
if string_to_search in line:
30+
print(line)
31+
file.close()
32+
33+
def seddoublespace(option, file_name):
34+
file = open(file_name)
35+
file_content = file.readlines()
36+
file.close()
37+
with open(file_name, "w") as f:
38+
for line in file_content:
39+
f.write(line+"\n\n")
40+
41+
def seddel(option, filename):
42+
print("hello")
43+
option_list = option.split("/")
44+
with open(filename, "r") as f_read:
45+
data = f_read.read().rstrip("\n")
46+
with open(filename, "w") as f_write:
47+
f_write.write(data[:-1])
48+
49+
################################ Script starts from here ################################
50+
## It uses alias ccsed="python ccsed.py"
51+
## take this into consideration when accepting command line arguments
52+
53+
if sys.argv[1] == "-n":
54+
options = sys.argv[2]
55+
#print(sys.argv[1], sys.argv[2])
56+
try: #ccsed -n '2,4p' file_name
57+
options_list = options.split(",")
58+
sedprint(sys.argv[1], options_list[0],options_list[1][0], sys.argv[3])
59+
except: #ccsed -n /roads/p unquoted.txt
60+
options_list = options.split("/")
61+
print(options_list)
62+
sedpatternprint(sys.argv[1], options_list[1],options_list[2], sys.argv[3])
63+
elif sys.argv[1].upper() == "G":
64+
file_name = sys.argv[2]
65+
seddoublespace(sys.argv[1], file_name)
66+
67+
elif sys.argv[1].split("/")[1] == "^$":
68+
seddel(sys.argv[1], sys.argv[2])
69+
70+
else: #ccsed s/this/that/g file_name
71+
options = sys.argv[1]
72+
file_name = sys.argv[2]
73+
options_list = options.split("/")
74+
if options_list[0].lower() == "s":
75+
try:
76+
if options_list[3].lower() != " ":
77+
substitution(file_name, "s", options_list[1], options_list[2], options_list[3].lower())
78+
except IndexError:
79+
substitution(file_name, "s", options_list[1], options_list[2])
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Your heart is the size of an ocean. Go find yourself in its hidden depths.
2+
The Bay of Bengal is hit frequently by cyclones. The months of November and May, in particular, are dangerous in this regard.
3+
Thinking is the capital, Enterprise is the way, Hard Work is the solution.
4+
If You Can'T Make It Good, At Least Make It Look Good.
5+
Heart be brave. If you cannot be brave, just go. Love's glory is not a small thing.
6+
It is bad for a young man to sin; but it is worse for an old man to sin.
7+
If You Are Out To Describe The Truth, Leave Elegance To The Tailor.
8+
O man you are busy working for the world, and the world is busy trying to turn you out.
9+
While children are struggling to be unique, the world around them is trying all means to make them look like everybody else.
10+
These Capitalists Generally Act Harmoniously And In Concert, To Fleece The People.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Life isn’t about getting and having, it’s about giving and being.
2+
Whatever the mind of man can conceive and believe, it can achieve.
3+
Strive not to be a success, but rather to be of value.
4+
Two roads diverged in a wood, and I—I took the one less traveled by, And that has made all the difference.
5+
I attribute my success to this: I never gave or took any excuse.
6+
You miss 100% of the shots you don’t take.
7+
I’ve missed more than 9000 shots in my career. I’ve lost almost 300 games. 26 times I’ve been trusted to take the game winning shot and missed. I’ve failed over and over and over again in my life. And that is why I succeed.
8+
The most difficult thing is the decision to act, the rest is merely tenacity.
9+
Every strike brings me closer to the next home run.
10+
Definiteness of purpose is the starting point of all achievemen

0 commit comments

Comments
 (0)