Sponsored Content
Full Discussion: For loop to break apart word
Top Forums Shell Programming and Scripting For loop to break apart word Post 302778361 by Don Cragun on Sunday 10th of March 2013 03:52:13 PM
Old 03-10-2013
Here is another example of how to do this that uses a function to split strings into arrays and then compares the array elements. This script uses several extensions to the standards, but works with recent versions of both ksh and bash:
Code:
#!/bin/ksh
# Usage: tryme string1 string2 [stringX stringX+1]...
# DESCRIPTION:
#       The tryme utility will demonstrate the use of the split2() shell
#       function by calling it twice for each pair of operands supplied.  It
#       then reports whether each pair of characters in the corresponding
#       strings match.
split2() {
# Usage: split2 arrray_name string
# DESCRIPTION:
#       The split2 function will split string into 2 character pairs and assign
#       those pairs to elements of the array specified by array_name with
#       subscripts starting with 1.  If string consists of an odd number of
#       characters, the last element assigned to the array will only contain
#       one character.
        s2_tmp2=1
        for((s2_tmp1 = 0; s2_tmp1 < ${#2}; s2_tmp1 += 2))
        do
                eval $1[$s2_tmp2]=${2:$s2_tmp1:2}
                s2_tmp2=$((s2_tmp2 + 1))
        done
}
while [ $# -ge 2 ]
do
        printf "Processing %s and %s\n" "$1" "$2"
        split2 v1 "$1"
        split2 v2 "$2"
        if [ ${#v1[@]} -ge ${#v2[@]} ]
        then    m=${#v1[@]}
        else    m=${#v2[@]}
        fi
        for((i = 1; i <= m; i++))
        do      printf "v1[%d]=%s, v2[%d]=%s\n" $i "${v1[$i]}" $i "${v2[$i]}"
                if [[ "X${v1[$i]}" == "X${v2[$i]}" ]]
                then    printf "v1[%d] is the same as v2[%d]\n" $i $i
                else    printf "v1[%d] is not the same as v2[%d]\n" $i $i
                fi
        done
        shift 2
done

An example using this script is:
Code:
tryme excellency exbeuukknn 12345 abc

which produces the output:
Code:
Processing excellency and exbeuukknn
v1[1]=ex, v2[1]=ex
v1[1] is the same as v2[1]
v1[2]=ce, v2[2]=be
v1[2] is not the same as v2[2]
v1[3]=ll, v2[3]=uu
v1[3] is not the same as v2[3]
v1[4]=en, v2[4]=kk
v1[4] is not the same as v2[4]
v1[5]=cy, v2[5]=nn
v1[5] is not the same as v2[5]
Processing 12345 and abc
v1[1]=12, v2[1]=ab
v1[1] is not the same as v2[1]
v1[2]=34, v2[2]=c
v1[2] is not the same as v2[2]
v1[3]=5, v2[3]=
v1[3] is not the same as v2[3]

This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to break the while loop???(Very Urgent)

H, I am running the following log.sh shell script. $no_of_ps=7 while do echo "hello $no_of_ps" ps_file=`tail -$no_of_ps /tmp/A380_RFS24/test.ls | head -1` no_of_ps=`expr $no_of_ps - 1` echo "package is: $ps_file" >> /tmp/A380_RFS24/log/A380_RFS24.log ps_file1=`echo $ps_file| sed... (1 Reply)
Discussion started by: sunitachoudhury
1 Replies

2. Shell Programming and Scripting

Weird loop break,- please Help

Hi all, im doing this script in which i read from a logfile line by line, my problem is this: The script was working fine until i added this statement to SSH into another machine to look for some data, it enters and retrieves the data just fine, but for some strange reason after it goes thru the... (1 Reply)
Discussion started by: sx3v1l_1n51de
1 Replies

3. Shell Programming and Scripting

How to break a loop if condition is met

I am having trouble figuring this code I want to grep a text from a file and if it match certain text it break out of the loop or it should continue searching for the text Here is what I have written but it isn't working while true f=`grep 'END OF STATUS REPORT' filename` do if ... (9 Replies)
Discussion started by: Issemael
9 Replies

4. Shell Programming and Scripting

break while loop in BASH

Hi gurus, I have the following part of code which I am using for treating input #!/bin/bash while ]; do arg=$1; shift case $arg in -u) users="$1" shift ;; -g) groups="$1" shift ;; ... (4 Replies)
Discussion started by: wakatana
4 Replies

5. Shell Programming and Scripting

sed: break before word if it's not last on the line

I've been trying this, and can't get it right. I want to put a line break before a word, but only if it's *not* the last word in the line. So if the break work was "fish," then... We want to fish tomorrow ...would become... We want to fish tomorrow ...but this line would remain... (3 Replies)
Discussion started by: estebandido
3 Replies

6. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

7. Shell Programming and Scripting

Line break on word

I have a file that contains the following: ^field LINE_1 data ^field LINE_2 data ^field LINE_3 data ^field LINE_4 data ^field LINE_5 data ... And im looking to do a line break at the end of the number before the text to make it look like this ^field LINE_1 ... (11 Replies)
Discussion started by: darbs121
11 Replies

8. Shell Programming and Scripting

Break the outer loop

Hi I'm comparing same files names which are in different folders . The first for loop for the files in DAY1 folder and the second for loop for the files in DAY2 folder . the first IF condition is for checking whether the file names are equal the second If condtion is for checking the... (4 Replies)
Discussion started by: smile689
4 Replies

9. Shell Programming and Scripting

Basic FOR loop with break

Oracle Linux : 6.4/bash shell In the below I want to break out of the loop when it enters the 5th iteration. #!/bin/bash for i in 1 2 3 4 5 6 do echo "$i" if echo "Oh Nooo... i = $i. I need to stop the iteration and jump out of the loop" then break fi done But, it only... (3 Replies)
Discussion started by: John K
3 Replies

10. Shell Programming and Scripting

Break in for loop

in my python script i have loop like below: for item in itemlist: if <condition>: <code> else: <code> if <condition>: if <condition>: <code> else: for type in types: if... (1 Reply)
Discussion started by: ctrld
1 Replies
array(n)						       Tcl Built-In Commands							  array(n)

__________________________________________________________________________________________________________________________________________________

NAME
array - Manipulate array variables SYNOPSIS
array option arrayName ?arg arg ...? _________________________________________________________________ DESCRIPTION
This command performs one of several operations on the variable given by arrayName. Unless otherwise specified for individual commands below, arrayName must be the name of an existing array variable. The option argument determines what action is carried out by the command. The legal options (which may be abbreviated) are: array anymore arrayName searchId Returns 1 if there are any more elements left to be processed in an array search, 0 if all elements have already been returned. SearchId indicates which search on arrayName to check, and must have been the return value from a previous invocation of array startsearch. This option is particularly useful if an array has an element with an empty name, since the return value from array nextelement won't indicate whether the search has been completed. array donesearch arrayName searchId This command terminates an array search and destroys all the state associated with that search. SearchId indicates which search on arrayName to destroy, and must have been the return value from a previous invocation of array startsearch. Returns an empty string. array exists arrayName Returns 1 if arrayName is an array variable, 0 if there is no variable by that name or if it is a scalar variable. array get arrayName ?pattern? Returns a list containing pairs of elements. The first element in each pair is the name of an element in arrayName and the second element of each pair is the value of the array element. The order of the pairs is undefined. If pattern is not specified, then all of the elements of the array are included in the result. If pattern is specified, then only those elements whose names match pat- tern (using the matching rules of string match) are included. If arrayName isn't the name of an array variable, or if the array contains no elements, then an empty list is returned. array names arrayName ?pattern? Returns a list containing the names of all of the elements in the array that match pattern (using the matching rules of string match). If pattern is omitted then the command returns all of the element names in the array. If there are no (matching) elements in the array, or if arrayName isn't the name of an array variable, then an empty string is returned. array nextelement arrayName searchId Returns the name of the next element in arrayName, or an empty string if all elements of arrayName have already been returned in this search. The searchId argument identifies the search, and must have been the return value of an array startsearch command. Warning: if elements are added to or deleted from the array, then all searches are automatically terminated just as if array done- search had been invoked; this will cause array nextelement operations to fail for those searches. array set arrayName list Sets the values of one or more elements in arrayName. list must have a form like that returned by array get, consisting of an even number of elements. Each odd-numbered element in list is treated as an element name within arrayName, and the following element in list is used as a new value for that array element. If the variable arrayName does not already exist and list is empty, arrayName is created with an empty array value. array size arrayName Returns a decimal string giving the number of elements in the array. If arrayName isn't the name of an array then 0 is returned. array startsearch arrayName This command initializes an element-by-element search through the array given by arrayName, such that invocations of the array nex- telement command will return the names of the individual elements in the array. When the search has been completed, the array done- search command should be invoked. The return value is a search identifier that must be used in array nextelement and array done- search commands; it allows multiple searches to be underway simultaneously for the same array. | array unset arrayName ?pattern? | Unsets all of the elements in the array that match pattern (using the matching rules of string match). If arrayName isn't the name | of an array variable or there are no matching elements in the array, then an empty string is returned. If pattern is omitted and is | it an array variable, then the command unsets the entire array. KEYWORDS
array, element names, search Tcl 8.3 array(n)
All times are GMT -4. The time now is 06:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy