How to print elements between letters bash script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print elements between letters bash script?
# 1  
Old 12-17-2014
How to print elements between letters bash script?

Hello to all,

May somebody help me to fix my bash code below, I'd like to do it only using loops and if statements is possible.

I the content of the arrays are represented by the following 2 strings, where each character is an element inside the array.

String for case 1:
Code:
81UV78UV183UV89800

String for case 2:
Code:
8U1UVV78UUV18UU3UV89800

The strings are separated by "U" followed by "V", this is "UV", but sometimes U or V appears alone (not in sequence UV). I want to print the elements between UV concatenated in a single string.

For String 1 the array is 1rst one and the output would be:
Code:
81
78
183
89800

and I'm only getting 81, 78 and 89800.

for String 2 the array is 2nd one "commented" and the output would be:
Code:
8U1
V78U
18UU3
89800

and I'm only getting 8, 78 and 18.

The code I have so far is:
Code:
#!/bin/bash
 
#String 1 (81UV78UV183UV89800) represents content of  this array
a[0]=8;a[1]=1;a[2]=U;a[3]=V;a[4]=7;a[5]=8;a[6]=U;
a[7]=V;a[8]=1;a[9]=8;a[10]=3;a[11]=U;a[12]=V;
a[13]=8;a[14]=9;a[15]=8;a[16]=0;a[17]=0;
 
#String 2 (8U1UVV78UUV18UU3UV89800) represents content of  this array
#a[0]=8;a[1]=U;a[2]=1;a[3]=U;a[4]=V;a[5]=V;a[6]=7;a[7]=8;
#a[8]=U;a[9]=U;a[10]=V;a[11]=1;a[12]=8;a[13]=U;a[14]=U;a[15]=3;a[16]=U;
#a[17]=V;a[18]=8;a[19]=9;a[20]=8;a[21]=0;a[22]=0;
 
for (( i=0; i<${#a[@]}; i++))
do
 if [ "${a[$i]}" == "U" ]; then
  c=1
 elif [ "${a[$i]}" == "V" ]; then
  c=0
  if (( ${#buff} > 0 )); then
   echo ${buff}
   buff=""
  fi
 elif (( c == 0 )); then
  buff=${buff}${a[$i]}
 fi
done

PS: The data is stored in arrays initially but I show you the way the data looks in a string for you to understand better. I'm using Cygwin.

Thanks in advance for any help.

Last edited by jim mcnamara; 12-17-2014 at 05:35 PM..
# 2  
Old 12-17-2014
Arrays are not a very good way to store this data you could simplify the code by using strings:

Code:
STR=81UV78UV183UV89800
for val in ${STR//UV/ }
do
   echo $val
done

Anyway here is the method you want - note the final test as you have no terminating UV in your data:

Code:
#!/bin/bash
 
#String 1 (81UV78UV183UV89800) represents content of  this array
#a[0]=8;a[1]=1;a[2]=U;a[3]=V;a[4]=7;a[5]=8;a[6]=U;
#a[7]=V;a[8]=1;a[9]=8;a[10]=3;a[11]=U;a[12]=V;
#a[13]=8;a[14]=9;a[15]=8;a[16]=0;a[17]=0;
a=( 8 1 U V 7 8 U V 1 8 3 U V 8 9 8 0 0 )
 
#String 2 (8U1UVV78UUV18UU3UV89800) represents content of  this array
#a[0]=8;a[1]=U;a[2]=1;a[3]=U;a[4]=V;a[5]=V;a[6]=7;a[7]=8;
#a[8]=U;a[9]=U;a[10]=V;a[11]=1;a[12]=8;a[13]=U;a[14]=U;a[15]=3;a[16]=U;
#a[17]=V;a[18]=8;a[19]=9;a[20]=8;a[21]=0;a[22]=0;
#a=(  8 U 1 U V V 7 8 U U V 1 8 U U 3 U V 8 9 8 0 0 )

for (( i=0; i<${#a[@]}; i++))
do
 if [ "${a[i]}${a[i+1]}" == "UV" ]; then
    ((i++))
    if (( ${#buff} > 0 )); then
           echo ${buff}
           buff=""
    fi
 else
    buff=${buff}${a[i]}
 fi
done
if (( ${#buff} > 0 )); then
   echo ${buff}
fi


Last edited by Chubler_XL; 12-17-2014 at 06:07 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 12-17-2014
This is bash. You can do what you want with far less code.

Code:
#!/bin/bash

# this outer loop of the code is here to put the strings in array variables
# you do not have to do it this way

for i in 81UV78UV183UV89800  8U1UVV78UUV18UU3UV89800
do 
  declare -a  myarr=( $(echo "$i" | sed 's/UV/ /g')  )  # create the element of the array
  for (( i=0 ; i< ${#myarr[*]} ; i++ ))  # print the elements of the array
  do
     echo ${myarr[i]}
  done
done

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 12-17-2014
Thanks so much Chubler_XL for fix my issue. It works this time. Smilie

Thanks Jim for show another way to do it with bash.

Best regards
# 5  
Old 12-17-2014
far less code :
Code:
for i in 81UV78UV183UV89800  8U1UVV78UUV18UU3UV89800; do  read -a ar <<<"${i//UV/ }"; printf '%s\n' "${ar[@]}"; done

Smilie
# 6  
Old 12-17-2014
Hi,

If you don't mind an AWK solution:

Code:
for str in 81UV78UV183UV89800  8U1UVV78UUV18UU3UV89800
do
echo $str | awk -F'UV' '{for (i=1;i<=NF;i++)if($i != "")print $i}'
done
81
78
183
89800
8U1
V78U
18UU3
89800

# 7  
Old 12-17-2014
Some more awk

Code:
 awk 'gsub(/UV/,"\n")' <<<$str

Code:
 awk '{print $1}' RS="UV" <<<$str

Code:
 awk '{$1=$1}1' OFS="\n" FS="UV" <<<$str

O/P with sample input

Code:
$ echo 81UV78UV183UV89800 | awk 'gsub(/UV/,"\n")'
81
78
183
89800

$ echo 8U1UVV78UUV18UU3UV89800 | awk 'gsub(/UV/,"\n")'
8U1
V78U
18UU3
89800

$ echo 81UV78UV183UV89800 | awk '{print $1}' RS="UV"
81
78
183
89800

$ echo 8U1UVV78UUV18UU3UV89800  | awk '{print $1}' RS="UV"
8U1
V78U
18UU3
89800

$ echo 81UV78UV183UV89800 | awk '{$1=$1}1' OFS="\n" FS="UV"
81
78
183
89800

$ echo 8U1UVV78UUV18UU3UV89800  | awk '{$1=$1}1' OFS="\n" FS="UV"
8U1
V78U
18UU3
89800


Last edited by Akshay Hegde; 12-17-2014 at 11:00 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inaccurate scanning of Bash array elements

username=cogiz #!/bin/bash shuffle() #@ USAGE: shuffle { #@ TODO: add options for multiple or partial decks Deck=$( printf "%s\n" {2,3,4,5,6,7,8,9,T,J,Q,K,A}{H,S,D,C} | awk '## Seed the random number generator BEGIN { srand() } ## Put a random number in front... (4 Replies)
Discussion started by: cogiz
4 Replies

2. UNIX for Beginners Questions & Answers

Random choice of elements in bash array

example of problem: #!/bin/bash P=(2 4 7) How would you randomly choose one of these 3 numbers in this array? either 2 or 4 or 7 is needed...but only one of them. Thanks in advance Cogiz Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: cogiz
3 Replies

3. UNIX for Beginners Questions & Answers

Scanning array for partial elements in Bash Script

Example of problem: computerhand=(6H 2C JC QS 9D 3H 8H 4D) topcard=6D How do you search ${computerhand} for all elements containing either a "6" or a "D" then save the output to a file? This is a part of a Terminal game of Crazy 8's that I'm attempting to write in Bash. Any... (2 Replies)
Discussion started by: cogiz
2 Replies

4. Shell Programming and Scripting

Select / Print odd-even elements of an array

Hello experts, I wish to print the contents of odd-even numbered indices of an array. The problem statement is as follows : 1. The first line contains an integer, (the number of test cases). 2. Each line of the subsequent lines containing a string. Example: 2 Haider Bash ... (4 Replies)
Discussion started by: H squared
4 Replies

5. UNIX for Dummies Questions & Answers

How to declare an array in UNIX and print the elements with tab delimits?

Hello, In a shell script, I want to declare an array and subsequently print the elements with tab delimits. My array has the following structure and arbitrary elements: myArray=('fgh' 'ijk' 'xyz' 'abc'); Next, I would like to print it with a '\n' at the end. Thanks for your input! ... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

6. Shell Programming and Scripting

Print arguments using array elements not working?

Hey guys, I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array... (16 Replies)
Discussion started by: shaner
16 Replies

7. UNIX for Dummies Questions & Answers

Grep /Awk letters X - X in every line and print it as a mac address

hey i m kinda new to this so i will appreciate any help , i have this list of values: pwwn = 0x50012482009cd7a7 nwwn=0x50012482009cd7a6 port_id = 0x280200 pwwn = 0x5001248201bcd7a7 nwwn=0x5001248201bcd7a6 port_id = 0x280300 pwwn = 0x50012482009c51ad nwwn=0x50012482009c51ac port_id =... (4 Replies)
Discussion started by: boaz733
4 Replies

8. Shell Programming and Scripting

How can I print array elements as different columns in bash?

Hi Guys, I have an array which has numbers including blanks as follows: 1 26 66 4.77 -0.58 88 99 11 12 333 I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7... (4 Replies)
Discussion started by: npatwardhan
4 Replies

9. Shell Programming and Scripting

tr | letters to symbol, bash scripting

hi Im trying to make the 'response' return the answer in the form of a dash (-) rather than the actuall letters of a given word typed in, this is what i have tried, but im not getting the dashes come through just a blank screen, any ideas guys? function enterWord () { echo "select to be... (2 Replies)
Discussion started by: ferrycorsten73
2 Replies

10. UNIX for Dummies Questions & Answers

Print last 2 letters in a word

Hi, I have a file which has the below information tm.orbit72 tm.orbit12 tm.orbit78 tm.orbitye I want to print the last two letters in the above file. Please let me know how can i do that. (6 Replies)
Discussion started by: Krrishv
6 Replies
Login or Register to Ask a Question