replacing single space in argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing single space in argument
# 1  
Old 04-24-2007
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 02:54 PM..
# 2  
Old 04-24-2007
to input arguments with spaces, on the command line use double quotes
eg
Code:
# ./script arg1 "hello test" blah blah

# 3  
Old 04-24-2007
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  
Old 04-25-2007
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  
Old 04-25-2007
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  
Old 04-25-2007
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  
Old 04-25-2007
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.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue handling single quoted argument in shell script.

Below is my script that works fine and prints the desired output: #!/bin/ksh echo "$1" | while IFS= read -r dirpath do echo "DIRR_PATH:$dirpath" install_dir=$install_dir" "$dirpath done echo "Desired Output:$install_dir" Output: ./loopissue.sh... (10 Replies)
Discussion started by: mohtashims
10 Replies

2. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

3. Shell Programming and Scripting

Passing multiple value in a single argument in Perl.

Hi all, I have below code through which trying to pick data from specific columns strating from a certain row. #!/usr/bin/perl #This script is to pick the specific fields from a files starting from a specific row # FILE -> Name of the file to be pasd at runtime. # rn -> Number of the... (4 Replies)
Discussion started by: Abhisrajput
4 Replies

4. Shell Programming and Scripting

Getopts - space in argument (OPTARG)

Hi, I want to capture space as well from the argument eg: script.ksh -m "Message from xyz" -e "email@xyz.com" script.ksh -m 'Message from xyz' -e 'email@xyz.com' I am parsing using getopts, but for option "m" OPTARG is returning only "Message". Please use code tags next time for... (9 Replies)
Discussion started by: tostay2003
9 Replies

5. UNIX for Dummies Questions & Answers

Replacing double spaces with single space

I am looking for a regular expression that uses sed to replace multiple spaces with single spaces on every line where it is not at the start of the line and not immediately before double slashes ('//') or between quotes ("). In its simplest form, it would look like this: sed -e 's# # #g'... (4 Replies)
Discussion started by: figaro
4 Replies

6. Shell Programming and Scripting

Replacing trailing space with single quote

Platform : RHEL 5.8 I want to end each line of this file with a single quote. $ cat hello.txt blueskies minnie mickey gravity snoopyAt VI editor's command mode, I have used the following command to replace the last character with a single quote. ~ ~ ~ :%s/$/'/gNow, the lines in the... (10 Replies)
Discussion started by: John K
10 Replies

7. Shell Programming and Scripting

Stripping out more than a space from a line, but keep single space.

Hi all, Is there a way to perform the above, I am trying to strip out more than one space from a line, but keep the single space. See below output example. My Name is test test2 test3 test4 test5 My Name is test test2 test3 test4 test5 Please note that the lines would contain... (7 Replies)
Discussion started by: eo29
7 Replies

8. Shell Programming and Scripting

getopts : two values to a single argument ?

Hi Gurus I am trying to figure out (with not much success) how to pass two values to a single getopts argument ... for example ./script -a Tuesday sausagesThe $OPTARG variable seems to only get populated with the first argument. What im looking to do is to process the first argument (i.e.make... (6 Replies)
Discussion started by: rethink
6 Replies

9. Shell Programming and Scripting

getopts ... only allow a single instance of an argument?

Hi there, if i have a simple getopts like below ...how can i make it so that if somebody enters more than one -g argument for example, it will error with a " you cannot enter more than one -g" or something like that.? I want to only allow one instance of a -g or a -h etc .. while getopts... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

10. Shell Programming and Scripting

Replacing a single quote

Hi there I have a data file like so below 'A/1';'T100002';'T100002';'';'01/05/2004';'31/05/2004';'01/06/2004';'08/06/2004';'1.36';'16';'0.22';'0';'0';'1.58';'0';'0';'0';'0';'0';'0';'clientes\resumen\200405\resumen_T100002_T100002_1.pdf';'';'0001';'S';'20040501';'';'02';'0';'S';'N'... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question