shift numbers to left in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shift numbers to left in a variable
# 1  
Old 07-19-2012
shift numbers to left in a variable

hi,

i have the below contents in a variable

Code:
echo $var
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121

what i want to achieve is everytime i run a command i want to shift numbers to left deleting first number in the series...

that is i want to to retrieve first number in the variable everytime so i will be using below command
Code:
echo $var | awk -F' ' '{print $1}'

so when i do that i will retrieve 1 but next time when i execute same command i want to get 3 and next time 5 and so on.... so to do that i need to shift numbers to left no matter how many digits each number has...

any help is deeply appreciated :-)
# 2  
Old 07-19-2012
Code:
$ var="1 3 5 7 9 11 13 15 17 19 21 23 ..."
$ set -- $var
$ shift
$ var=$@
$ echo $var
3 5 7 9 11 13 15 17 19 21 23 ...

You could maybe encapsulate it into a function for easier use.
This User Gave Thanks to Scott For This Post:
# 3  
Old 07-19-2012
.......

Last edited by guruprasadpr; 07-19-2012 at 03:56 AM.. Reason: Already provided
# 4  
Old 07-19-2012
thanks scott,... above code works like a jem :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shift command help

#!/bin/bash hostname=$1; shift for hostname in $1 do ping $hostname done I want to run the above script as hostname.sh yahoo.com google.com cnn.com. I want to shift each hostname to $1. How can do that with above code as currently it's not shifting. (5 Replies)
Discussion started by: scj2012
5 Replies

2. Shell Programming and Scripting

Comparing a list of numbers is less than a variable

Hello everyone, I want to compare a list of numbers in the file TEST01 to the variable $Post. Also remove any duplicate numbers. Create an if then statement indicating if the numbers listed in TEST01 is less than the number value of $Post then print an error message. Here is the contents of... (5 Replies)
Discussion started by: seekryts15
5 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

Perl match multiple numbers from a variable similar to egrep

I want to match the number exactly from the variable which has multiple numbers seperated by pipe symbol similar to search in egrep.below is the code which i tried #!/usr/bin/perl my $searchnum = $ARGV; my $num = "148|1|0|256"; print $num; if ($searchnum =~ /$num/) { print "found"; }... (2 Replies)
Discussion started by: kar_333
2 Replies

5. Shell Programming and Scripting

Assign numbers with decimals from a line to a variable

I am trying to assign a string of numbers with their decimals to a variable. The code reads a file called 'log'. I have not included the entire file since it's huge. The line is: Tagging release with the label program-2.8.114...My code: BUILDNUMFORSIT=$(egrep 'Tagging release with the... (3 Replies)
Discussion started by: sgffgs
3 Replies

6. UNIX for Dummies Questions & Answers

appending running numbers on a variable

hi guys, would appreciate some help here. I need to append running numbers using sed onto a variable that contain a list of IP addresses. I'm basically stuck on the running number part. e.g. 1. 10.0.0.1 2. 10.0.0.2 3. 10.0.0.3 (10 Replies)
Discussion started by: jazzaddict
10 Replies

7. UNIX for Dummies Questions & Answers

A Shift into UNIX

Hi, Firstly, I did a search for this question both on this site and on the internet and have not been able to find a suitable answer that is not general in nature. I have always been a Windows user. I use my girl friend's mac every now and then, but I always come back to windows. For a... (1 Reply)
Discussion started by: mearex
1 Replies

8. UNIX for Dummies Questions & Answers

Variable Names With Numbers

Hi Everyone, I was just curious if you are allowed to begin a variable name with a number. For example, I would really like to have the variable 8DAY_AVERAGE. But my shell script only seems to allow EIGHTDAY_AVERAGE. Is there a way I can get the former to work? I am using bash. Thanks a lot! ... (1 Reply)
Discussion started by: msb65
1 Replies

9. UNIX for Dummies Questions & Answers

left padding numbers

Hi, can someone please tell me how to left-pad numbers using unix. e.g. 1234 -> 00001234 Thanks in advance for your help. (1 Reply)
Discussion started by: colquhoi
1 Replies

10. Shell Programming and Scripting

shift command

Hi Folks, In shell scripting the maximum no. of command line parameters becomes 9(Am i right). If we want to get more than 9 parameters we use the shift command. Even here there are two possibilities. 1. Without the use of variables - The arguments are lost and the lost no. is equal to the... (6 Replies)
Discussion started by: Nisha
6 Replies
Login or Register to Ask a Question