1

My python program requires a system call with the following command.

"cat "+transDir+transFile+" | grep \""+fileName+" \" | cut -d\" \" -f2- | sed \"s/ (/=/g\" | cut -d\"=\" -f1 | sed \"s/) /=/g\" | cut -d\"=\" -f2- | sed \"s/''" 

The string turns out to be something like this

#look at the last two characters, ie. ''
'cat ../results/allTrans.txt | grep "Sentence_L3_1 " | cut -d" " -f2- | sed "s/ (/=/g" | cut -d"=" -f1 | sed "s/) /=/g" | cut -d"=" -f2- | sed "s/'\'\''    

As you can see, the last '' has become \'\' in the string. How do I get just ' ' ?

6
  • 5
    it is just displayed that way (because '' are used to display string literal), the actual content of the string did not change. Do a print some_string to see the actual string. Commented Jan 6, 2017 at 11:33
  • 1
    You're looking at the repr() of the string. Do as @Rusty suggested and you'll see that the string is fine. Commented Jan 6, 2017 at 11:35
  • What Rusty said. Your string really has two ' at the end. BTW, using cat to feed a file to grep is an anti-pattern, just give the file arg to grep directly. Commented Jan 6, 2017 at 11:35
  • did you run it first ? Did you get any error message or get any problem ? Commented Jan 6, 2017 at 11:36
  • i would hardly recomment you to check the string format method Commented Jan 6, 2017 at 11:55

4 Answers 4

5

In plain English: String literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

enter image description here

\' is used to escape ' in case it confused with 'this is your command' the python string enclosing single quotes. Just ignore it.

Sign up to request clarification or add additional context in comments.

1 Comment

That cleared things up, thanks. Man I love stackoverflow!
1

Try this:

print 'cat ../results/allTrans.txt | grep "Sentence_L3_1 " | cut -d" " -f2- | sed "s/ (/=/g" | cut -d"=" -f1 | sed "s/) /=/g" | cut -d"=" -f2- | sed "s/''\'\''

Comments

1

String Literal notation (which usually is an input for some program to communicate with outside) is not the String itself (which is what it really is in memory) is what you really need to understand to avoid getting confused.

'\n'   = New Line   = Binary(12)
'\\'   = \          = Binary(92)
'\''   = '          = Binary(44)
'\x80' = <Depends*> = Binary(128) 

The left hand side is what you need to give as Input to Python to understand that. Right hand side is what it actually is inside the memory.

The reason we need this is because some characters (called as Control Characters) like Line Feed cannot be directly entered using Keyboard and often has different meanings to the host application, so most languages define a way to accept these characters using \ as prefix and then some suffix which is usually easy to memorize. \ itself needs to be given as \\.

And if you started your string literal with ' which has a special meaning, you need to give it as \' so as to not confuse with the ending of the string literal notation.

* It depends on the encoding.

Comments

0

Try putting r in front of the last string like so:

r" \" | cut -d\" \" -f2- | sed \"s/ (/=/g\" | cut -d\"=\" -f1 | sed \"s/) /=/g\" | cut -d\"=\" -f2- | sed \"s/''"

or if that doesn't work, you can try with triple quotes:

""" \" | cut -d\" \" -f2- | sed \"s/ (/=/g\" | cut -d\"=\" -f1 | sed \"s/) /=/g\" | cut -d\"=\" -f2- | sed \"s/''"""

1 Comment

It would be great if you explain what raw strings are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.