So I decide to pick up a different language that is more intuitive. After googling around, I decided to give Python a try. I mainly write scripts to manipulate files and strings, So file IO and regular expression are the first two things I checked out.
First let's create a file called abc:
$ echo "Hello perl" > abc
Using sed, we can easily replace 'perl' with 'python'
$ sed 's/perl/python/' abc
To do it in Python, we can use this script
#!/usr/bin/pythonimport osimport reimport stringfr = open('abc', 'r')mystr = fr.read()fr.close()str_by_sub = re.sub('perl', 'python', mystr)
str_by_replace = string.replace(mystr, 'perl', 'python')fw = open('def','w')fw.write(mystr)fw.write(str_by_sub)fw.write(str_by_replace)fw.close()
After executing the script, we will have a file called def, the content would be
There are other parameters with string.replace and re.sub, which control the behaviors of the replacement, I still need further reading to understand them better.Hello perlHello pythonHello python
It was very intuitive to write my first Python script, I think I will like Python :)