Python sed simulation to add ##

Source: Internet
Author: User

In our normal work, we sometimes need to operate on a file, such as adding ##in front of each line of a file. In shell, this requirement is very simple, you can use the sed single line to solve the problem. Let's take a look at a file:

[Root @ host-192-168-209-128 py-sed] # cat a.txt
This is a text
This is use for python
This is also user for sed
This is a end test file
[Root @ host-192-168-209-128 py-sed] #
It is very easy to use sed's single-line command to solve this requirement. Let's look at the Code:
[Root @ host-192-168-209-128 py-sed] # sed's/^/#/G' a.txt
# This is a text
# This is use for python
# This is also user for sed
# This is a end test file
[Root @ host-192-168-209-128 py-sed] #

Let's take a look at the powerful sed. Next I will introduce you to how to use python to implement this operation that is often needed, and directly go to the Code:
[Root @ host-192-168-209-128 py-sed] # cat a. py
#! /Usr/bin/env python

With open('a.txt ') as f:
Con = f. readlines ()
For I in range (0, len (con )):
Print "###" + con [I]. rstrip ('\ n ')
The code is really simple. Let's see how it works:
[Root @ host-192-168-209-128 py-sed] # python a. py
### This is a text
### This is use for python
### This is also user for sed
### This is a end test file

Haha, the effect is coming out, but there is a slight defect. We write the object file to be operated in the Code. How can we pass the file name as a parameter to the script? We need to modify it, to implement the following functions:
1. the file to be operated must be passed as a parameter to the script
2. Determine whether the operation object exists.
3. If the script runs incorrectly, a friendly prompt is required.
Based on the above requirements, the final version of the code is provided. The Code is as follows:

[Root @ host-192-168-209-128 py-sed] # cat tou. py
#! /Usr/bin/env python
'''
Edit by qhz
Email: world77@163.coom
This scrip to add "###" at every line for file

'''
Def usage ():
Print '''
========================================================== ========
This script to add "###" at every line for file
Use Example:
Python script. py file
========================================================== ========
'''
Import sys
Import OS
If len (sys. argv) = 2:
If OS. path. isfile (sys. argv [1]):
With open (sys. argv [1]) as f:
Con = f. readlines ()
For I in range (0, len (con )):
Print '###' + con [I]. strip ('\ n ')
Else:
Print "============================================ =========="
Print "Your input file name is not exit or not correct"
Print "Please try again, bye ..."
Print "============================================ =========="
Else:
Usage ()
Exit ()
[Root @ host-192-168-209-128 py-sed] #

Let's take a look at the various situations and effects:
[Root @ host-192-168-209-128 py-sed] # python tou. py

========================================================== ========
This script to add "###" at every line for file
Use Example:
Python script. py file
========================================================== ========

[Root @ host-192-168-209-128 py-sed] # python tou. py a. tx
========================================================== ========
Your input file name is not exit or not correct
Please try again, bye...
========================================================== ========
[Root @ host-192-168-209-128 py-sed] # python tou. py a.txt
### This is a text
### This is use for python
### This is also user for sed
### This is a end test file
[Root @ host-192-168-209-128 py-sed] #


Now, the introduction of python is here. I will simulate some simple sed functions for you one after another.


This article from "you are a passer-by or huoyuanjia" blog, please be sure to keep this source http://world77.blog.51cto.com/414605/1348328

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.