Visit Our UNIX and Linux User Community


Set problem with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Set problem with spaces
# 1  
Old 09-07-2013
Set problem with spaces

Code:
#### ~]$ set "hello 'cat dog walk' money elephat"

Code:
#### ~]$ echo $* | awk '{print $2}'
'cat

why is the second command above showing only
Code:
"'cat 

?

shouldn't the output be:

Code:
'cat dog walk'

how can i fix this so it gives me the chosen column in its entirety?
# 2  
Old 09-07-2013
Do you want to put the string(hello 'cat dog walk' money elephat) in a variable?:
Code:
$ v="hello 'cat dog walk' money elephat"
$ echo $v
hello 'cat dog walk' money elephat

# 3  
Old 09-07-2013
Quote:
Originally Posted by spacebar
Do you want to put the string(hello 'cat dog walk' money elephat) in a variable?:
Code:
$ v="hello 'cat dog walk' money elephat"
$ echo $v
hello 'cat dog walk' money elephat

in this particular instance, i need to use set to set the variables. it looks as though the set command has issues accepting values between quotes as one single value.

Last edited by SkySmart; 09-07-2013 at 01:08 AM..
# 4  
Old 09-07-2013
If I understand what you are trying to do this is another way:
Code:
IFS=","
myvars="hello,cat dog walk,money,elephat"
for v in $myvars
do
  echo $v
done

This User Gave Thanks to spacebar For This Post:
# 5  
Old 09-07-2013
Quote:
Originally Posted by SkySmart
Code:
#### ~]$ set "hello 'cat dog walk' money elephat"

Code:
#### ~]$ echo $* | awk '{print $2}'
'cat

why is the second command above showing only
Code:
"'cat 

?

shouldn't the output be:

Code:
'cat dog walk'

how can i fix this so it gives me the chosen column in its entirety?
The command:
Code:
set "hello 'cat dog walk' money elephat"

sets $# to 1 and $1 to hello 'cat dog walk' money elephat.

The command:
Code:
echo $* | awk '{print $2}'

with its default field separator set to split fields on combinations of one or more spaces or tabs has field 1 set to hello, field 2 set to 'cat, field 3 set to dog, etc. and the awk command should print:
Code:
'cat

I have no idea how you got a leading " nor a trailing space from that command.

Without awk, the following shell commands might do what you want:
Code:
set hello 'cat dog walk' money elephat
printf "args=%d, arg1=%s, arg2=%s, arg3=%s, arg4=%s\n" $# "$1" "$2" "$3" "$4"

should print:
Code:
args=4, arg1=hello, arg2=cat dog walk, arg3=money, arg4=elephat

assuming that you're using a standards conforming shell such as bash or ksh.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 09-07-2013
Try
Code:
set hello 'cat dog walk' money elephat
echo "$@" | awk '{print $2}'

As Don suggested, simpler than the awk thing is
Code:
echo "$2"


Last edited by MadeInGermany; 09-07-2013 at 03:14 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 09-07-2013
@MadeinGermany: that will provide cat. To use awk ( instead of printf "%s\n" "$2" ) we need to get around the field splitting of awk with something like:
Code:
printf "%s\n" "$@" | awk '{print $2}' FS='\n' RS=

or just:
Code:
printf "%s\n" "$@" | awk NR==2


Last edited by Scrutinizer; 09-07-2013 at 03:30 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #12
Difficulty: Easy
CPU stands for 'Computer Processing Unit' and controls all of a computer's functions, acting as the brain(s) of computers.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

"set variable with spaces and apostrophe after s"

Hi Guys, I have a variable such that: set x = (Session,Date,Type,Receive Coil Name,Manufacturer,Manufacturer's Model Name) foreach i ($x) echo $i end I would like to read each variable one by one like: Session Date Type Receive Coil Name Manufacturer Manufacturer's Model Name Is... (1 Reply)
Discussion started by: dixits
1 Replies

2. Shell Programming and Scripting

Problem with spaces in the path

Hi all, I have a variable test has the following value assigned.. could you please help on doing cd or ls to the value in the varible ... $echo $test /bdm/sdd/compounds/AD4833XT/requests/clin/Watson_20090420/docs/MHRA\ Comments\ \&\ Responses $cd $test ksh: cd: bad argument count $cd... (3 Replies)
Discussion started by: firestar
3 Replies

3. Shell Programming and Scripting

problem with spaces in filename

I have written a script to run ddrescue on a list of files. #!/bin/bash # # A script to rescue data recursively using ddrescue. srcDir=/damaged/hdd/movies/ #the source directory desDir=/new/hdd/movies/ #the destination directory... (2 Replies)
Discussion started by: colsinc
2 Replies

4. Web Development

[MYSQL] problem with spaces in rows

Hello. I'm not sure how I can get around this, or what I am doing wrong, but I need some help. :) I want to do an select query looking like this: SELECT venue, SUM( amount ) FROM IWD WHERE venue = 'Foxy Hollow' Unfortunately I need to have spaces in the names in these fields, is... (10 Replies)
Discussion started by: noratx
10 Replies

5. UNIX for Dummies Questions & Answers

Problem with White spaces and tabs

Hi All, I am facing issues converting white spaces and tabs together in a file I am reading. Here is the command I am trying: tr -s ' '@ | sort -t@ +1n filename I guess the problem is that it is not converting the tabs to another delimiter. Also, I am supposed to accomplish this only using... (5 Replies)
Discussion started by: sh_kk
5 Replies

6. Shell Programming and Scripting

Problem using egrep: spaces and newline

Hello: I am working in bash and am a newbie. I want to eliminate spaces from strings. Since this is a basic operation, I searched online and implemented the suggestions; however, I am facing a problem here. I have an input file which looks like this: abc defghi jklmno pqrs tuvw xyzabcd... (8 Replies)
Discussion started by: andyu11
8 Replies

7. Shell Programming and Scripting

set tab to 4 spaces in vi

Hi, I want to set my tab lenght to 4 spaces instead of 8. And when i press tab instead of inserting tab it should insert 4 spaces. if i do set ts=4 this set tab=4. But this inserts tab. Say suppose i copy the code from unix to texpad/wordpad.Textpad will interpret tab as 8 spaces.(I can set... (4 Replies)
Discussion started by: pinnacle
4 Replies

8. UNIX for Dummies Questions & Answers

Problem with spaces in directory path

Hi Gurus, I have a requirement. cat /usdd/Sample/"NDDF Plus DB"/"NDDF Descriptive and Pricing"/"NDDF BASICS 3.0"/"Pricing"/1.txt | sed 's/*|*/|/g' | sed 's/^*//'| sed 's/^*//; s/*$//' > temp.txt In unix prompt the above command is reading the file 1.txt and I am... (1 Reply)
Discussion started by: prabhutkl
1 Replies

9. Shell Programming and Scripting

problem with spaces and argument parsing

public class HelloWorld { public static void main(String args) { System.out.println("Welcome, master"); } } and I compiled using javac HelloWorld.java ] Suppose that I execute the following command directly from the shell: java -XX:OnError="gdb - %p" HelloWorld Then it works... (8 Replies)
Discussion started by: fabulous2
8 Replies

10. UNIX for Dummies Questions & Answers

problem with the blank spaces in array

Hi all! i need your help. I'm getting started with this... in need to insert in an array string values. But the thing is that this strings have blank spaces... for example if a want to put "filed1 = " and "field2 = " .... in the array , when i want to print all the fields, it only shows... (4 Replies)
Discussion started by: kamicasse
4 Replies

Featured Tech Videos