LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2009, 02:28 AM   #1
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Rep: Reputation: 30
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
 
Old 08-13-2009, 02:45 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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.

Last edited by catkin; 08-13-2009 at 03:32 AM.
 
Old 08-13-2009, 04:31 AM   #3
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by catkin View Post
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 cath. 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
 
Old 08-13-2009, 09:25 AM   #4
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,665

Rep: Reputation: 485Reputation: 485Reputation: 485Reputation: 485Reputation: 485
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.
 
Old 08-13-2009, 02:40 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Thanks malekmustaq
Quote:
Originally Posted by malekmustaq View Post
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
 
Old 08-13-2009, 06:13 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
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 .
 
Old 08-13-2009, 10:45 PM   #7
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
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.
 
Old 08-13-2009, 11:01 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 08-14-2009, 06:09 AM   #9
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by pixellany View Post
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
 
Old 08-14-2009, 07:10 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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.
 
Old 08-14-2009, 08:15 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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?

Last edited by pixellany; 08-14-2009 at 08:16 AM.
 
Old 08-14-2009, 08:50 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by catkin View Post
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!

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Buzz Word Today Is Change LXer Syndicated Linux News 0 02-26-2008 03:50 PM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 09:46 PM
how to change a word in a text file EXnihilo Linux - General 11 09-11-2006 08:07 AM
change word during boot bruse Linux - Newbie 4 02-11-2005 08:24 AM
Java: Change first character in word to upper AMMullan Programming 4 04-05-2004 03:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration