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 03-09-2010, 02:26 PM   #1
vouser
LQ Newbie
 
Registered: Mar 2010
Posts: 10

Rep: Reputation: 0
Shell script: I have string "abc____def____ghi", how to make "abc def ghi"


It's silly, but I could not find examples for this.
I need to replace N number of _ with N number of spaces.
It always makes me "abc def ghi". (One space gap)
Thanks to all.
 
Old 03-09-2010, 02:49 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Is this what you want:
Code:
echo $foo
abc____def____ghi

$ echo $foo | sed 's/_/ /g'
abc    def    ghi
Hope this helps.
 
Old 03-09-2010, 03:09 PM   #3
vouser
LQ Newbie
 
Registered: Mar 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Thank you very much for trying to help me.
Here are more details on my issue.

I have a script with a variable:

msg_string='01009999888811000001E00000001000099999988881004102099999999999900__USA_________123456789 012345'

I need a code that replaces all underscores with blanks preserving the length of the message.

I ran your code and it gave me:
01009999888811000001E00000001000099999988881004102099999999999900 USA 123456789012345

As you see, we altered the message length.

Thanks again,
vouser.
 
Old 03-09-2010, 03:12 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by druuna View Post
Hi,

Is this what you want:
Code:
echo $foo
abc____def____ghi

#      V    V
$ echo "$foo" | sed 's/_/ /g'
abc    def    ghi
Hope this helps.
Use quotes, otherwise bash interprets the spaces as argument separators.

Last edited by MTK358; 03-09-2010 at 03:13 PM.
 
Old 03-09-2010, 03:26 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

@MTK358: There are no spaces in the input

@vouser: If you apply my solution I do think you have what you want:
Code:
$ foo='01009999888811000001E00000001000099999988881004102099999999999900__USA_________123456789 012345'
$ echo $foo
01009999888811000001E00000001000099999988881004102099999999999900__USA_________123456789 012345
$ echo $foo | sed 's/_/ /g'
01009999888811000001E00000001000099999988881004102099999999999900  USA         123456789 012345
That is what you ask:
Quote:
I need a code that replaces all underscores with blanks preserving the length of the message.
Hope this helps.
 
Old 03-09-2010, 03:49 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by druuna View Post
There are no spaces in the input
Oh, I just got a little confused.

I would still use the braces in case the string contains bash special chars, though.
 
Old 03-09-2010, 04:41 PM   #7
vouser
LQ Newbie
 
Registered: Mar 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Gentlemen:

thanks for your help:

Here's my working function:

fn_send_msg(){
msg_out=`msg_get 1``msg_get 2``msg_get 3``msg_get 4``msg_get 12``msg_get 13``msg_get 14``msg_get 18``msg_get 37``msg_get 39``msg_get 59``msg_get 62`
echo "Msg before replacing:"
echo $msg_out

#msg_out=`echo $msg_out | sed 's/_/ /g'` #~ OK
msg_out="${msg_out//_/ }" #~ OK
echo "Msg out:"
echo "$msg_out"

#~ Open socket ~~~~~~~~~~~~~~~~~~~~
exec 3<>/dev/tcp/172.26.0.103/9991

#~ Send msg ~~~~~~~~~~~~~~~~~~~~~~~
echo "$msg_out" >&3

#~ Receive msg ~~~~~~~~~~~~~~~~~~~~
read -r msg_in <&3
echo "Msg in:"
echo "$msg_in" # OK
}

What I've learned is that there is a difference between:

echo $msg

and

echo "$msg"

if msg has blanks.

Regards,
vouser
 
Old 03-09-2010, 08:20 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
since you are using bash, you can use this function to replace your strings without calling external commands.

Code:
repl(){
  mystring="$1"
  r="$2"
  sub="$3"
  echo "${mystring//$r/$sub}" #thanks to catkin jolting my memory to quote my variables

}
use it like this in your script.
Code:
repl "abc____def____ghi__end" "_" " "

Last edited by ghostdog74; 03-09-2010 at 10:13 PM.
 
Old 03-09-2010, 10:01 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Bash's Parameter Expansion can do the job quite cleanly:
Code:
c@CW8:~$ string='abc____def____ghi'
c@CW8:~$ echo "${string//_/ }"
abc    def    ghi
From the link above:

${parameter/pattern/string} The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced.
 
  


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
Regular expression to extract "y" from "abc/x.y.z" rag84dec Linux - Newbie 1 05-29-2008 02:47 AM
converting shell script from "dialog" to "xdialog" kushalkoolwal Programming 1 02-25-2008 08:40 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM


All times are GMT -5. The time now is 10:49 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