Skip to content

Commit 2e79506

Browse files
committed
sed quivalent: step 6 and challenge completed
1 parent a6e1772 commit 2e79506

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Coding Challenge#21[build sed]/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ to support deletion
2929

3030
$ ccsed /^$/d unquoted.txt
3131
<br>########## ================== <br>
32+
33+
### Stepp: 6
34+
inplace edit and save
35+
36+
$ ccsed -i 's/Life/Code/g' unquoted.txt
37+

Coding Challenge#21[build sed]/ccsed.py

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import re
22
import sys
33

4-
def substitution(file_name, option, what_to_sub, what_to_sub_with, operate_on = "g"):
4+
def substitutionWithoutSave(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+
print(file_modified)
11+
# f.seek(0, 0)
12+
# f.write(file_modified)
13+
# f.truncate()
14+
15+
def substitutionWithSave(file_name, option, what_to_sub, what_to_sub_with, operate_on = "g"):
516
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}")
617
with open(file_name, "r+") as f:
718
file = f.read()
@@ -51,6 +62,7 @@ def seddel(option, filename):
5162
## take this into consideration when accepting command line arguments
5263

5364
if sys.argv[1] == "-n":
65+
print("-n")
5466
options = sys.argv[2]
5567
#print(sys.argv[1], sys.argv[2])
5668
try: #ccsed -n '2,4p' file_name
@@ -61,19 +73,33 @@ def seddel(option, filename):
6173
print(options_list)
6274
sedpatternprint(sys.argv[1], options_list[1],options_list[2], sys.argv[3])
6375
elif sys.argv[1].upper() == "G":
76+
print("G")
6477
file_name = sys.argv[2]
6578
seddoublespace(sys.argv[1], file_name)
6679

67-
elif sys.argv[1].split("/")[1] == "^$":
80+
elif sys.argv[1] == "-i": ## ccsed -i s/abc/def/g file_name
81+
print("calling sed without saving")
82+
options = sys.argv[2].split("/")
83+
file_name = sys.argv[3]
84+
if options[0].lower() == "s":
85+
try:
86+
if options[3].lower() != " ":
87+
substitutionWithSave(file_name, "s", options[1], options[2], options[3].lower())
88+
except IndexError:
89+
substitutionWithSave(file_name, "s", options[1], options[2])
90+
91+
elif sys.argv[1].split("/")[1] == "^$": ## ccsed /^$/d unquoted.txt
92+
print("calling seddel")
6893
seddel(sys.argv[1], sys.argv[2])
6994

7095
else: #ccsed s/this/that/g file_name
96+
print("sed without save")
7197
options = sys.argv[1]
7298
file_name = sys.argv[2]
7399
options_list = options.split("/")
74100
if options_list[0].lower() == "s":
75101
try:
76102
if options_list[3].lower() != " ":
77-
substitution(file_name, "s", options_list[1], options_list[2], options_list[3].lower())
103+
substitutionWithoutSave(file_name, "s", options_list[1], options_list[2], options_list[3].lower())
78104
except IndexError:
79-
substitution(file_name, "s", options_list[1], options_list[2])
105+
substitutionWithoutSave(file_name, "s", options_list[1], options_list[2])
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Life isn’t about getting and having, it’s about giving and being.
1+
Code isn’t about getting and having, it’s about giving and being.
22
Whatever the mind of man can conceive and believe, it can achieve.
33
Strive not to be a success, but rather to be of value.
44
Two roads diverged in a wood, and I—I took the one less traveled by, And that has made all the difference.
@@ -7,4 +7,4 @@ You miss 100% of the shots you don’t take.
77
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.
88
The most difficult thing is the decision to act, the rest is merely tenacity.
99
Every strike brings me closer to the next home run.
10-
Definiteness of purpose is the starting point of all achievemen
10+
Definiteness of purpose is the starting point of all achievement

0 commit comments

Comments
 (0)