Strings to integers?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Strings to integers?
# 1  
Old 12-22-2013
Strings to integers?

Hi,

I'm totally new at this, so help will be appreciated.

I have a directory with a bunch of files in it. The files are named xinteger_yinteger_zinteger.vtk (eg, x3_y0_z-1.vtk). I want to read the filenames and then assign the integers to variables that I then can use in expressions. So, for x3_y0_z-1.vtk, I want to assign xvariable=3, yvariable=0, zvariable-1.

I assume that I have to use one command to assign a filename to a string and then something like printf to parse the string. But that's as far as I've gotten.

Help?

Thanks!
# 2  
Old 12-22-2013
Let me see if I have got this right.

You want to extract the filename only and use the_values_ in the filename itself?

Do I take it you are not interested in the contents of the file itself?
# 3  
Old 12-22-2013
Correct. I'm concerned only with the file names.
# 4  
Old 12-22-2013
This solution is based on some assumptions (using parameter substitution):
Code:
#!/bin/bash

for file in *.vtk
do
        x="${file%%_*}"
        x="${x//[a-z]}"

        y="${file#*_}"
        y="${y%_*}"
        y="${y//[a-z]}"

        z="${file##*_}"
        z="${z%.*}"
        z="${z//[a-z]}"

        echo $x $y $z
done

This User Gave Thanks to Yoda For This Post:
# 5  
Old 12-22-2013
Assuming that you're using a POSIX conforming shell (such as ksh or bash), the following seems to do what you want:
Code:
#!/bin/ksh
for file in x*_y*_z*.vtk
do
        printf "%s\n" "$file" | (
                IFS="xyz_." read x xvariable x yvariable x zvariable x
                printf "Processing file %s: x=%s, y=%s, z=%s\n" "$file" \
                        "$xvariable" "$yvariable" "$zvariable"
        )
done

When the above script is run in a directory containing the files:
Code:
problem
tester
x1_y2_z3.vtk
x3_y0_z-1.vtk

the output produced is:
Code:
Processing file x1_y2_z3.vtk: x=1, y=2, z=3
Processing file x3_y0_z-1.vtk: x=3, y=0, z=-1

These 3 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-22-2013
This is longhand using OSX 10.7.5, default bash terminal...
Some assumptions have been made.

Code:
Last login: Sun Dec 22 22:12:49 on ttys000
AMIGA:barrywalker~> echo "junk stuff" > /tmp/x3_y0_z-1.vtk
AMIGA:barrywalker~> echo "junk stuff" > /tmp/x6_y0_z-5.vtk
AMIGA:barrywalker~> text=$(ls /tmp/*vtk)
AMIGA:barrywalker~> echo "$text"
/tmp/x3_y0_z-1.vtk
/tmp/x6_y0_z-5.vtk
AMIGA:barrywalker~> ifs_str="$IFS"
AMIGA:barrywalker~> IFS="$IFS/_."
AMIGA:barrywalker~> array=($text)
AMIGA:barrywalker~> x1="${array[2]:1:2}"
AMIGA:barrywalker~> y1="${array[3]:1:2}"
AMIGA:barrywalker~> z1="${array[4]:1:2}"
AMIGA:barrywalker~> echo "x=$x1, y=$y1, z=$z1"
x=3, y=0, z=-1
AMIGA:barrywalker~> x2="${array[7]:1:2}"
AMIGA:barrywalker~> y2="${array[8]:1:2}"
AMIGA:barrywalker~> z2="${array[9]:1:2}"
AMIGA:barrywalker~> echo "x=$x2, y=$y2, z=$z2"
x=6, y=0, z=-5
AMIGA:barrywalker~> IFS="$ifs_str"
AMIGA:barrywalker~> _

# 7  
Old 12-22-2013
Quote:
Originally Posted by jhsinger
I'm totally new at this, so help will be appreciated.
Great! You are in for learning the most interesting thing there is the world: shell programming in the Unix environment!

(Well, now that i jog my memory really hard, i have to admit there are a few other interesting activities too in life - but nothing is that rewarding, i promise. ;-)) )

Quote:
Originally Posted by jhsinger
I have a directory with a bunch of files in it. The files are named xinteger_yinteger_zinteger.vtk (eg, x3_y0_z-1.vtk). I want to read the filenames and then assign the integers to variables that I then can use in expressions. So, for x3_y0_z-1.vtk, I want to assign xvariable=3, yvariable=0, zvariable-1.

I assume that I have to use one command to assign a filename to a string and then something like printf to parse the string. But that's as far as I've gotten.
You won't need "printf", but i appreciate you doing your own thinking. Lets go through it step by step:

First, we want to read in all the filenames in a directory. We do that in a loop:

Code:
#! /bin/ksh

typeset file=""

ls /path/to/your/dir |\
while read file ; do
     print - "we have found file ${file}"
done

exit 0

We define a variable "file" and in every pass of the loop this variable is set to a new value, cycling through all the file names in your directory. Once we are through it the loop ends and so does your script. The "print"-line is in there only for your edification: control if all the files you want to process are indeed shown and if every file you wanted to be left out is indeed left out. If this is not the case you might want to change your filemask, for instance:

Code:
ls /path/to/your/dir/*certain-extension |\
while read file ; do

otherwise you are ready to proceed: Now that we have the filename available we are ready to dissect it. In the shell this is done by so-called "variable expansion". You should indeed read a good book about shell programming which will cover this perhaps in a chapter of its own. Its a most often neglected art but one of the differences between the pros and the crowd. The reason: it might look awkward at first, but it is blazingly fast. You might need several lines, but these are executed faster by an order of magnitude than a single line calling some external utility.

Basically you can cut off patterns from a string from the front or the end. The first one is really easy:

Code:
file="x3_y0_z-1.vtk"
print - ${file%%.vtk}

You will see that the extension ".vtk" is removed. "%%" means: take the variables ("file") content and cut off from the end everything that matches the pattern (".vtk") and display what is left over. Let us put that into our script:

Code:
#! /bin/ksh

typeset file=""

ls /path/to/your/dir/*vtk |\
while read file ; do
     file="${file%%.vtk}"
     print - "we have now filename ${file}"
done

exit 0

Again, the "print"-statement serves no other purpose than to let you see what we have achieved so far. Now, the main work: let us first chop off "z". For this we use an "inverted %%", the "##". It removes a pattern beginning from the start of a string. Because we know that the value we want to know is at the end, following a "z", we can simply remove everything up to this letter:

Code:
file="x3_y0_z-1"
print - ${file##*_z}

The "*" means the same as in "ls -l *": any number of characters in any length. Let us put that into the script:

Code:
#! /bin/ksh

typeset file=""
typeset -i x=0
typeset -i y=0
typeset -i z=0

ls /path/to/your/dir/*vtk |\
while read file ; do
     file="${file%%.vtk}"
     z=${file##*_z}
     
     print - "Our x: $x  our y: $y    our z: $z"
done

exit 0

Likewise we are ready to get the other variables: first, we cut off the already extracted z-part, then we apply the same logic as before for the y-part:

Code:
file="x3_y0_z-1"
z=-1
file=${file%%z${z}}
print - "${file}"

Try these things on the command line. You can also play around with the patterns and the variable contents to get a feeling for this. Finally we put that into the script again:

Code:
#! /bin/ksh

typeset file=""
typeset -i x=0
typeset -i y=0
typeset -i z=0

ls /path/to/your/dir/*vtk |\
while read file ; do
     file="${file%%.vtk}"
     z=${file##*_z}
     file=${file%%z${z}}
     y=${file##*_y}
    
     print - "Our x: $x  our y: $y    our z: $z"
done

exit 0

I am sure by now you don't my explanations any more and you surely are eager to apply your newfound knowledge on the last part of the problem. Have fun with shell programming.

I hope that helps.

bakunin
These 3 Users Gave Thanks to bakunin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing Integers (I think)

Hi, I can't figure out what I'm missing. I'm running a query to see if there are any streams recording on my DVR before starting a scripted update. I'm guessing that it is viewing $VIDEO as a string instead of an int. I've tried everything I saw on google but it still comes back as $VIDEO is... (8 Replies)
Discussion started by: Rhysers
8 Replies

2. Shell Programming and Scripting

Bash Integers/String

Hy friends, I am newbie to bash scripting, can anyone explain how b=${a/23/BB} # Substitute "BB" for "23". this line converts "b" into string and and "d" into Integer. Thanks in advance (4 Replies)
Discussion started by: Qazi
4 Replies

3. Shell Programming and Scripting

awk -- telling the difference between strings and integers

This should be a really easy question. My input file will have a few fields that are strings in the first line, which I will extract and save as variables. The rest of the fields on every line will be integers and floating point numbers. Can awk tell the difference somehow? That is, is there... (5 Replies)
Discussion started by: Parrakarry
5 Replies

4. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

5. Shell Programming and Scripting

Comparison treating strings as zero integers

I'm trying to write a bash script to perform basic arithmetic operations but I want to run a comparison on the arguments first to check that they're a number greater than zero. I want an error to pop up if the arguments args aren't >= 0 so I have: if ! ]; then echo "bad number: $1" fi ... (14 Replies)
Discussion started by: TierAngst
14 Replies

6. Shell Programming and Scripting

Cancel down 2 integers

Wonderful evening to all of you! My problem has to possible starting points. Well, not really, but getting to either one is no problem at all. So i got either a string in the format of "1920x1080" or simply the integers X = 1920 and Y = 1080. When I am done, I would like to have an output... (5 Replies)
Discussion started by: jakunar
5 Replies

7. Shell Programming and Scripting

Strings to integers in an arithmetic loop

Hi all, Could someone please advise what is the correct syntax for my little script to process a table of values? The table is as follows: 0.002432 20.827656 0.006432 23.120364 0.010432 25.914184 0.014432 20.442655 0.018432 20.015243 0.022432 21.579517 0.026432 18.886874... (9 Replies)
Discussion started by: euval
9 Replies

8. Shell Programming and Scripting

Add non-integers using ksh

I would like to add 4.7 and 1.2. However I am unable to do this with expr. Any simple ideas (even using something other than expr)? Example: me> expr 4 + 1 5 me> expr 4.7 + 1.2 expr: 0402-046 A specified operator requires numeric parameters. (18 Replies)
Discussion started by: 2dumb
18 Replies

9. Programming

Using write() with integers in C

I'm trying to write an integer to a file using the write() function, but write() requires the parameter to be written to be a const void*. How would I go about doing this? also: using itoa() produces a " warning: implicit declaration of function 'itoa' " even though i have #included stdlib.h (2 Replies)
Discussion started by: h@run
2 Replies

10. Shell Programming and Scripting

integers in the if statement

hi, im trying to compare two variables in csh to put in an if statement, eg: set a = $firstnum set b = $secondnum if ($a -ge $b) echo $a But I get an error ("if: Expression syntax"). How can I make csh see my variables as integers? thanks in advance! (5 Replies)
Discussion started by: Deanne
5 Replies
Login or Register to Ask a Question