Visit Our UNIX and Linux User Community


The last argument contains spaces, how do I get it into a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting The last argument contains spaces, how do I get it into a variable?
# 1  
Old 10-19-2011
The last argument contains spaces, how do I get it into a variable?

Gooday
I have an argument string that contains 15 arguments.
The first 14 arguments are easy to handle because they are separated by spaces
Code:
ARG14=`echo ${ARGSTRING} | awk '{print $14}'`

The last argument is a text that may be empty or contain spaces. So any ideas on how I get the last argument into the variable ARG15? ie delete from $ARG14 to the beginning of line will be greately appreciated.

regards

Zagga


Moderator's Comments:
Mod Comment Please use code tags

Last edited by zaxxon; 10-19-2011 at 08:40 AM.. Reason: code tags, see PM
# 2  
Old 10-19-2011
Something like this?
Code:
ARG15=`echo ${ARGSTRING} | awk '{sub(".*" $14 FS,x)}1'`

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 10-21-2011
Thanks very much Franklin52, your solution works fine while arguments 14 and 15 are populated. Unfortunately I omitted to mention that these fields are optional.
After testing: if Arg14 and Arg15 are null, then Arg15 contains the same value as Arg13.
If Arg15 only is null, the it recuperates the value of Arg14.
I figured I would add tests on each field to set to "NONE" if null, unless you have a better trick up your sleeve?

Regards

Zagga
# 4  
Old 10-21-2011
Quote:
Originally Posted by zagga
Thanks very much Franklin52, your solution works fine while arguments 14 and 15 are populated. Unfortunately I omitted to mention that these fields are optional.
After testing: if Arg14 and Arg15 are null, then Arg15 contains the same value as Arg13.
If Arg15 only is null, the it recuperates the value of Arg14.
I figured I would add tests on each field to set to "NONE" if null, unless you have a better trick up your sleeve?

Regards

Zagga
Do you have 2 spaces (field separators) at the end of the line if the last 2 fields are empty?
# 5  
Old 10-21-2011
NO in fact I have 56 spaces at the end of the record. But the good news is the last two fields are fixed length which is not the case of all the preceding fields. Arg14= 4 cars Arg15=50 cars + 2 FS=56.

So I need to get from the end of $Arg13 to the EoL into a new variable and then I should be able to cut Arg14 and 15 from their positions.

Can you do a cut backwards from the end of line?

Any ideas welcome?

Zagga
# 6  
Old 10-22-2011
Try this:
Code:
Arg14=`echo ${ARGSTRING} | awk '{print substr($14, 1 ,4)}'`

Arg15=`echo ${ARGSTRING} | awk '{print substr($14, 5 ,50)}'`

# 7  
Old 10-24-2011
Sorry Franklin52 that doesn't seem to work:

Case 1:
if both $14 and 15 are null ie full of spaces.
I get Arg14=" " and Arg15 has nothing.

Case 2:
if $14="3333" and $15 is null
I get Arg14="3333" and Arg15 has nothing.

Case 3:
if $14 is null ie " " and $15 contains "12345678901234567890123456789012345678901234567890"
I get Arg14="1234" and Arg15="567890......"

Case 4:
If $14="9999" and $15="one two three four five six seven ....."
I get Arg14="9999" and Arg15=

I think I need to create an intermediary variable and get everything from the end of $13 into it.

I'm not sure I've made myself clear.

Thanks in advance

Zagga

---------- Post updated at 11:51 AM ---------- Previous update was at 11:39 AM ----------

The reply I just posted may have been a little misleading as I quoted ARG14 but not ARG15. so output looked different. As it turns out Case 1 and 2 give expected results. Case 3 and 4 don't.
Zagga

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check for spaces in input argument!

Hi guys, I have created a csh script which allows user to pass input argument with the script like: cluster_on_lev3.csh -t <value> -p <value> Example: cluster_on_lev3.csh -t 2.3 -p 0.05 Now I want to create an error code where if user passes input argument without spaces , the code... (16 Replies)
Discussion started by: dixits
16 Replies

2. Shell Programming and Scripting

Passing variable as an argument to another script

Hi, I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem. varaiable "a" value should be -b "FPT MAIN". When we pass "a" to another script, we are expecing it to get substitue as ./test.sh -b "FPT MAIN". But, it is getting... (9 Replies)
Discussion started by: Manasa Pradeep
9 Replies

3. Shell Programming and Scripting

Variable passed as argument

I have a script. #!/bin/sh cur_$1_modify_time=Hello echo "cur_$1_modify_time" When I run like sh /root/script1 jj I expect value "Hello" being assigned to variable "cur_jj_modify_time" and output being "Hello" ie echoing $cur_jj_modify_time But the output comes as # sh... (3 Replies)
Discussion started by: anil510
3 Replies

4. Shell Programming and Scripting

pass variable as sar argument

I am trying to get the details of iowait for last hour using sar. When I give date as argument it works. # sar -u -s 22:24:00 -e 23:24:00 Linux 2.6.35.13-26 (XX.server.com) 04/11/2012 10:30:03 PM CPU %user %nice %system %iowait %idle 10:40:04 PM all 5.03 ... (2 Replies)
Discussion started by: anil510
2 Replies

5. Shell Programming and Scripting

Pass command line argument to variable

Hi, I had written a shell script to pass command line argument to variable in a function. Here is my code: main if ; then .$1 echo $1 get_input_file else echo "input file $1 is not available" fi get_input_file() { FILE = "$1" echo $FILE } (10 Replies)
Discussion started by: Poonamol
10 Replies

6. Shell Programming and Scripting

about an argument with variable in sed

hi all let say I run the ./xxx.bash x x x x or ./xxx.bash x x x or ./xxx.bash x x x x x the last argument always a filename the last arugment filename format is 5-10-22.txt my question is how can I put this arugment into variable and I can use it in sed or any other way that i can use I... (1 Reply)
Discussion started by: yuesko
1 Replies

7. Shell Programming and Scripting

how to assign script argument to a variable

I have a script file. test.sh I am running it by command sh test.sh 10102004 where 10102004 is the script argument. I wan to assign this 10102004 to a variable. How can i do this? I tried &1 and awks ARGV its not working :( (2 Replies)
Discussion started by: rohankit
2 Replies

8. 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

9. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

10. Shell Programming and Scripting

Passing the command line argument in a variable

Hi, I am new to unix. Is their a way to pass the output of the line below to a variable var1. ls -1t | head -1. I am trying something like var1=ls -1t | head -1, but I get error. Situation is: I get file everyday through FTP in my unix box. I have to write a script that picks up first... (1 Reply)
Discussion started by: rkumar28
1 Replies

Featured Tech Videos