LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-30-2007, 10:52 PM   #1
fjkum
Member
 
Registered: Feb 2006
Posts: 38

Rep: Reputation: 15
sed, replacing underscore with whitespace


hi, I am currently writting a bash script to replace a string with underscore '_' to a whitespace ' '.

How can I use sed to achieve that?

Below is my snippet of my script:

LONG_STRING_WITH_UNDERSCORE = "This_is_my_script"
LONG_STRING_WITH_WHITESPACE = `cat $LONG_STRING_WITH_UNDERSCORE | sed -e 's,_, ,g'`

echo $LONG_STRING_WITH_WHITESPACE

Is this the correct way?


FJ
 
Old 10-30-2007, 11:10 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
run the script and find out!
if there are errors, it means not correct, right?

hint: use echo instead of cat and see how it goes
 
Old 10-30-2007, 11:28 PM   #3
fjkum
Member
 
Registered: Feb 2006
Posts: 38

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
run the script and find out!
if there are errors, it means not correct, right?

hint: use echo instead of cat and see how it goes

Ran it and it gives me
"cat: This_is_my_script: No such file or directory."

What's wrong?
 
Old 10-31-2007, 12:09 AM   #4
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Code:
LONG_STRING_WITH_UNDERSCORE="This_is_my_script"
LONG_STRING_WITH_WHITESPACE=`echo $LONG_STRING_WITH_UNDERSCORE | sed -e 's,_, ,g'`

echo $LONG_STRING_WITH_WHITESPACE
Works fine for me. Notice not having whitespace around the "=" and using echo instead of cat.

You could also just use tr since all you're doing is substituting one char for another.
Code:
LONG_STRING_WITH_WHITESPACE=`echo $LONG_STRING_WITH_UNDERSCORE | tr '_' ' '`
#OR
LONG_STRING_WITH_WHITESPACE=$(tr '_' ' ' <<< $LONG_STRING_WITH_UNDERSCORE)
Whatever you find more readable.

Last edited by angrybanana; 10-31-2007 at 12:13 AM.
 
  


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
replacing sed -s option i.you Linux - Software 3 10-28-2007 12:08 PM
[SOLVED] Replacing using sed with variables from another file? eamesj Programming 5 11-24-2006 10:20 AM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM
Whitespace parsing sed? carl.waldbieser Programming 1 12-12-2005 04:24 PM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 10:33 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration