The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM
How to compare null and space using single if condition jayakumarrt UNIX for Dummies Questions & Answers 3 05-16-2008 05:13 AM
Replacing URL in a file with space dsrookie UNIX for Dummies Questions & Answers 5 02-29-2008 04:58 AM
Consecutive spaces within input being converted to single space NinersFan Shell Programming and Scripting 4 09-04-2007 08:59 AM
Replacing a single quote rjsha1 Shell Programming and Scripting 3 12-23-2005 10:55 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-24-2007
convenientstore convenientstore is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 42
replacing single space in argument

I want to write a script which will check the arguments and if there is a single space(if 2 more more space in a row , then do not touch), replace it with _ and then gather the argument

so, program will be ran
Code:
./programname  hi    hello hi    usa  now  hello hello
so, inside of program, after going through my little script, it should come out as

$1 = hi
$2 = hello_hi
$3 = usa
$4 = now
$5 = hello_hello


It's rather diffuclt for me since I am not sure even if I got rid of space and replace it with _, not sure how to assign back to correct positional parameters..

Last edited by reborg; 04-24-2007 at 01:54 PM..
  #2 (permalink)  
Old 04-24-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,512
to input arguments with spaces, on the command line use double quotes
eg
Code:
# ./script arg1 "hello test" blah blah
  #3 (permalink)  
Old 04-24-2007
convenientstore convenientstore is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 42
I am trying to run it so that user don't have to quote anything when they put argument(often times, it will be copy and paste)
  #4 (permalink)  
Old 04-25-2007
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
To simplify, pass everything as 1 quoted argument, then have the script process it, e.g.
Code:
$ cat > space_parsing
#!/bin/bash

set -- $( echo "$1" | sed 's/\([^ ]\) \([^ ]\)/\1_\2/g' | tr -s ' ' )

echo "$@"

echo "$1"

echo "$4"

exit 0
^D
$ chmod +x !$
chmod +x space_parsing
$ ./space_parsing "foo    bar    baz    hello hi    foo   hello hi again"
foo bar baz hello_hi foo hello_hi_again
foo
hello_hi
Cheers,
ZB
  #5 (permalink)  
Old 04-25-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by convenientstore
I want to write a script which will check the arguments and if there is a single space(if 2 more more space in a row , then do not touch), replace it with _ and then gather the argument

so, program will be ran
Code:
./programname  hi    hello hi    usa  now  hello hello
so, inside of program, after going through my little script, it should come out as

$1 = hi
$2 = hello_hi
$3 = usa
$4 = now
$5 = hello_hello


It's rather diffuclt for me since I am not sure even if I got rid of space and replace it with _, not sure how to assign back to correct positional parameters..

You cannot do that. Your script sees only the individual arguments; there will be no spaces. You can demonstrate that with:

Code:
printf "%s\n" "$@"
To preserve spaces, they must be quoted:

Code:
./programname  "hi    hello hi    usa  now  hello hello"
Better is to quote the arguments correctly:

Code:
./programname  hi "hello hi" usa now "hello hello"
  #6 (permalink)  
Old 04-25-2007
hankooknara hankooknara is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 80
thanks guys.. but I am writing this script so that end user can be as lazy as possible(since they are copying from somewhere and pasting it as arguments).

I thought maybe I can do it as put the whole argument as array.. and then after doing the replacing( " " to _ ) and then assign it back to $1,$2 .... ?

is this not possible in shell script?

so, my logic would be,

./scriptname whatever whatever1 whate ever2 whate ver 2006

so, script would see it as
@arrayname = $* # or possibly
value = $1,$2,$3,$4,$5,$6 <--i would insert extra just in case
and then possibly break it out using sed and awk.. but not sure how to assign it back...
newvalue =
  #7 (permalink)  
Old 04-25-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by hankooknara
thanks guys.. but I am writing this script so that end user can be as lazy as possible(since they are copying from somewhere and pasting it as arguments).

I thought maybe I can do it as put the whole argument as array.. and then after doing the replacing( " " to _ ) and then assign it back to $1,$2 .... ?

is this not possible in shell script?

No, it is not possible.

Each word on the command line is passed as a separate argument (to any program, not just shell scripts).

The only way to preserve whitespace is to quote it.

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0