Using Write command to create new syntax - problem with unwanted spaces - syntax

I am trying to run a command on a list of variables stored as values in a different file. To do that I am creating a new syntax based on the variable names, like this:
WRITE OUT="\Selection.sps"
/"VARIABLE ATTRIBUTE VARIABLES = Final_",Var1," ATTRIBUTE=selectVars('yes')." .
EXECUTE.
The Problem is between Final and Var1, I am getting 11 spaces. The file in which I want to use this macro has variable names as Final_Var1 (So in the new file, Final is added to each variable's Name). How can I remove the space so that the new variable can be referred to properly? Should I create a new file or COMPUTE and CONCAT commands?

The write command is limited like that - you can't avoid the spaces or use trim. For commands like the one you're working on there is no way to build the command within the write command - you have to build the text in advance and then put it in the write command, so -
strimg mycmd(a100).
compute mycmd=concat("VARIABLE ATTRIBUTE VARIABLES = Final_",
ltrim(string(Var1,f4)),
" ATTRIBUTE=selectVars('yes').").
WRITE OUT="\Selection.sps" /mycmd .
exe.
Note that this is not the only way to work on variable lists - you can use Python code within the syntax to build your variable lists more efficiently.

I have found a temporary solution, in order to remove the spaces from the variables, I am creating a new variable using:
*Add a variable to use in .sps file.
NUMERIC A(F4).
COMPUTE A = Var1.
ALTER TYPE A (A25).
STRING CMD (A100).
COMPUTE CMD = CONCAT("VARIABLE ATTRIBUTE VARIABLES = Final_", LTRIM (A) , ATTRIBUTE=selectVars('yes').").
EXECUTE.
WRITE OUT="File location\Selection.sps" /CMD.
EXECUTE.
and now a macro can be created using Selection.sps.
If a simpler way exists, please let me know!

Related

How to obtain variable values from a text file for use by a bash script?

I have a bash script, and I'd like it to read in the values of variables from a text file.
I'm thinking that in the text file where the values are stored, I'd use a different line for each variable / value pair and use the equals sign.
VARIABLE1NAME=VARIABLE1VALUE
VARIABLE2NAME=VARIABLE2VALUE
I'd then like my bash script to assign the value VARIABLE1VALUE to the variable VARIABLE1NAME, and the same for VARIABLE2VALUE / VARIABLE2NAME.
Since the syntax of the file is the syntax you would use in the script itself, the source command should do the trick:
source text-file-with-assignments.txt
alternately, you can use . instead of source, but in a case like this, using the full name is more clear.
The documentation can be found in the GNU Bash Reference Manual.

change file extension with a ruby script

I'm trying to change the exstension of a file passing the arguments by console
system = "rename" , "'s/\#{ARGV[0]}$/\#{ARGV[1]}'", "*#{ARGV[1]}"
The code is correct because it works on console but when I put it in a script I have trouble with
s/\#
because it appears in pink and the console does not get it.
you don't want to send literal single quotes, so remove them.
you want to remove the backslashes so you let Ruby evaluate those expressions.
you're missing the trailing slash.
what's that equal sign doing?
did you want ARGV[0] in the last argument to rename, instead of ARGV[1]?
you want to use * wildcard, which requires a shell to expand into a list of files, which means you can't use the list form of system
Try
system "/usr/bin/rename -n 's/#{ARGV[0]}$/#{ARGV[1]}/' *#{ARGV[0]}"
Remove the -n option if it looks like you're going to rename the way you want.
And, of course, you don't need to call out to the shell for this:
Dir.glob("*#{ARGV[0]}").each {|fname|
newname = fname.sub(/#{ARGV[0]}$/, ARGV[1])
File.rename(fname, newname)
}

Create directories with incrementing variables as part of directory name using Bash within for loop

I am trying to write a bash script which allows me to automate the creation of multiple directories with incrementing names.
For example, I am trying to create the directories named v0v1, v1v2, ... , v40v41.
I have tried using a 'for' loop, where I create a variable and set this to be equal to the current value of i+1 (where 'i' is the current loop iteration), but it is not working as expected.
I have managed to get the variable to increment (and have checked this using 'echo'), but I cannot get it to become part of the new directory name.
The code I have written is as follows:
for i in {0..40}; do let r=$((i+1)); mkdir v$iv$r; done
However, the directories produced have names only containing the first variable value (i.e. v0, v1, ..., v40), and do not include the 'v$r' at all.
Does anyone have any ideas how to use two variables at once in the same filename?
printf 'r=%d ; mkdir v$((r-1))v${r} ;' $(seq 2 41) |sh
You don't need a shell loop at all.
for i in {0..40}; do let r=$((i+1)); mkdir v${i}v$r; done
When bash sees the expression v$iv$r, it substitutes in for variables iv and r. But, of course, there is no iv. So bash substitutes in the empty string. The solution is to use braces to assure that bash knows where the variable name ends. Thus: v${i}v$r.

Shell Scripting: Using a variable to define a path

My problem lies with my confusion with shell variables.
To my understanding, variables allow me to store a value (String in this case) and to call it later in my code. So if I wanted to have a variable that holds the path to some set of scripts, I could ideally just store it like this:
SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'
//Later on in the script//
cd $SPTH
./script1
What I'm trying to do, with probably the wrong syntax, is to set the path to variable SPTH.
Then I use cd with argument $SPTH.
Ideally this would allow me to run the file there without typing in the path. However it doesn't work. The $SPTH is ignored and the result is as if cd was used alone.
So what am I doing wrong? And what would be a way to do this?
Don't use spaces...
(Incorrect)
SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'
(Correct)
SPTH='/home/Foo/Documents/Programs/ShellScripts/Butler'
To add to the above correct answer :-
For my case in shell, this code worked (working on sqoop)
ROOT_PATH="path/to/the/folder"
--options-file $ROOT_PATH/query.txt

Putting to a Filename named after a variable

I am using Put (>>) to store information that I am obtaining in Mathematica. The problem is that I am putting a several variables. However, if I do the following, it outputs to a file called year rather than my variable.
For example:
year=64;
sortedTally>>year;
This exports to a file named year rather than a file named 64. The documentation notes that expr >> filename is equivalent to expr >> "filename". Is there any way to circumvent this and put to a filename that changes based on the variables? This is similarly reiterated in the documentation file on Operator Input Forms (at the bottom).
In this case you need to use Put[sortedTally, ToString[year]].

Resources