LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Change word (https://www.linuxquestions.org/questions/linux-newbie-8/change-word-747204/)

elainelaw 08-13-2009 02:28 AM

Change word
 
I use the below command to change the string , it works,

sed -e "s/aaa/bbb/" file

can advise if now the string is not aaa , it is c:\path , what can i do ?

thx

catkin 08-13-2009 02:45 AM

See grymoire on the slash as a delimiter

Edit 1: you could see it; it's quite interesting, but it doesn't answer your question :(

Edit 2:

I wanted to give you a link to a page that explains, to help you help yourself, but have not been able to find one that explains clearly and is easy to understand.

Your aaa is a very simple "regular expression" meaning, er, aaa. Regular expressions are very powerful ways to describe strings of characters. Example: [hc]at matches "hat" and "cat". Here the [hc] means h or c.

But what if you wanted to find exactly [hc]at? Then you can remove the special meaning of [ and ] by "escaping" them with a backslash, like this: \[hc\]at. Now sed will look for exatly "[hc]at".

You want to find "c:\path". As a regular expression, c:\path means c:path. The \ has been used to remove any special meaning from p so the \ itself has disappeared!

What to do? You didn't want \ to be used that way, you wanted it to stand for itself. That's OK, you know how to remove the special meaning from a character by "escaping" it with a backslash. So give c:\\path to sed and it will look for c:\path.

elainelaw 08-13-2009 04:31 AM

Quote:

Originally Posted by catkin (Post 3641518)
See grymoire on the slash as a delimiter

Edit 1: you could see it; it's quite interesting, but it doesn't answer your question :(

Edit 2:

I wanted to give you a link to a page that explains, to help you help yourself, but have not been able to find one that explains clearly and is easy to understand.

Your aaa is a very simple "regular expression" meaning, er, aaa. Regular expressions are very powerful ways to describe strings of characters. Example: [hc]at matches "hat" and "cat". Here the [hc] means h or c.

But what if you wanted to find exactly [hc]at? Then you can remove the special meaning of [ and ] by "escaping" them with a backslash, like this: \[hc\]at. Now sed will look for exatly "[hc]at".

You want to find "c:\path". As a regular expression, c:\path means c:path. The \ has been used to remove any special meaning from p so the \ itself has disappeared!

What to do? You didn't want \ to be used that way, you wanted it to stand for itself. That's OK, you know how to remove the special meaning from a character by "escaping" it with a backslash. So give c:\\path to sed and it will look for c:\path.

it works , thx help

malekmustaq 08-13-2009 09:25 AM

elainelaw,

Just a point of encouragement.

Catkin has done a good job at explaining things that would have taken you about 10+ minutes to read from a good tutorial.

If I were you, I would give him/her the highest compliment by pressing that blue thumbs-up icon at the lower right corner of the post. That icon is our only currency here, acceptable as payment to any form of indebtedness in our work, something that money can't buy. :)

Well done catkin.

good luck elaine.

catkin 08-13-2009 02:40 PM

Thanks malekmustaq :)
Quote:

Originally Posted by malekmustaq (Post 3641904)
things that would have taken you about 10+ minutes to read from a good tutorial.

True but do you know where to find one? Normally grymoire is good but the stuff on REs is confusing even when you know them! And on escaping it is especially unclear, hence my post. Posting links to good intros or references is a good way to help people (give someone a fishing rod ...) so I'd like to have some good links.

Best

Charles

chrism01 08-13-2009 06:13 PM

Here's a site to interactively learn/test regexes.
http://www.regexbuddy.com/
Note that the regex rules are not(!) identical for every tool that does regexing ... sigh .. ;)
The best book is Mastering Regular Expressions, by Jeffrey Friedl; see http://regex.info/
Gets very high marks from Amazon, O'Reilly and me :) .

elainelaw 08-13-2009 10:45 PM

thx all suggestion , I will give a icon as you said once I fully solve my problem .

I have one more question , the command is works to run on the shell , but it is not in my application ,

I try sed -e "s/c:\/bbb/g" , it can replace c" to bbb on my application, but use sed -e "s/c:\\\aaa/bbb/g" is only work on shell but not in my application ( my application enivornment is quite complicate to me , I am not sure how to expliant the details )

But I think the problem is because it can not see c:\\\aaa as a string , I would like to ask if I can add something like a quote "" to it so that it see anything in the quote as a string ? or can anyone provide another method to replace the string ( other than sed -e "s/c:\\\aaa/bbb/g" file > file.new ) that I would try . thx in advance.

pixellany 08-13-2009 11:01 PM

What is the language of your application? If it is calling SED, that implies that it is a shell script.

Please post the exact commands you are using, an example of the input data, and the results you are getting..

Watch out for the backslash:
suppose you are in a situation where one "\" means to "escape" the character following. To get a literal "\" in that situation, you need "\\". Thus, "\\\" means literal "\", followed by the escaped value of the next character. So, in your example, "c:\\\a" means literal "c:\", and an escaped "a". The latter has no meaning in a sed command.

elainelaw 08-14-2009 06:09 AM

Quote:

Originally Posted by pixellany (Post 3642737)
What is the language of your application? If it is calling SED, that implies that it is a shell script.

Please post the exact commands you are using, an example of the input data, and the results you are getting..

Watch out for the backslash:
suppose you are in a situation where one "\" means to "escape" the character following. To get a literal "\" in that situation, you need "\\". Thus, "\\\" means literal "\", followed by the escaped value of the next character. So, in your example, "c:\\\a" means literal "c:\", and an escaped "a". The latter has no meaning in a sed command.


thx reply ,


my command is
sed -e "s/c:\\\aaa/bbb/g"


if run from my application , it will not do anything ( the exact command is unix silent sed -e "s/c:\\\aaa/bbb/g" ) , if I save this command to a script ( my_script ) , then run this program from my application , it is OK ( the excat command is unix silent my_script ) , can advise what is wrong in my case ? thx

catkin 08-14-2009 07:10 AM

I don't know excat but the symptoms suggest it is parsing the command in a similar manner to bash before handing it to bash.

pixellany 08-14-2009 08:15 AM

Quote:

Please post the exact commands you are using, an example of the input data, and the results you are getting..
Is this an actual command?
Code:

unix silent sed -e "s/c:\\\aaa/bbb/g"
(Please put code into [CODE] tags as I have done here-----click on the Advanced tab, and then select the code and click on "#")

If the above is actual code, then what is it supposed to do?

catkin 08-14-2009 08:50 AM

Quote:

Originally Posted by catkin (Post 3643089)
I don't know excat but the symptoms suggest it is parsing the command in a similar manner to bash before handing it to bash.

:rofl: Ha! Ha! Ha! I took elainelaw's "the excat command is unix silent my_script" to mean the command was issued from the excat environment! Only searching for excat did I realise it's a typo for exact! :D

I wonder why elainelaw hasn't told us which environment is being used to issue the command? Hens teeth and blood out of stones!


All times are GMT -5. The time now is 02:25 AM.