Sponsored Content
Top Forums Shell Programming and Scripting How to print elements between letters bash script? Post 302929095 by Ophiuchus on Wednesday 17th of December 2014 04:24:08 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
BASHBUG(1)						      General Commands Manual							BASHBUG(1)

NAME
bashbug - report a bug in bash SYNOPSIS
bashbug [--version] [--help] [email-address] DESCRIPTION
bashbug is a shell script to help the user compose and mail bug reports concerning bash in a standard format. bashbug invokes the editor specified by the environment variable EDITOR on a temporary copy of the bug report format outline. The user must fill in the appropriate fields and exit the editor. bashbug then mails the completed report to bug-bash@gnu.org, or email-address. If the report cannot be mailed, it is saved in the file dead.bashbug in the invoking user's home directory. The bug report format outline consists of several sections. The first section provides information about the machine, operating system, the bash version, and the compilation environment. The second section should be filled in with a description of the bug. The third sec- tion should be a description of how to reproduce the bug. The optional fourth section is for a proposed fix. Fixes are encouraged. ENVIRONMENT
bashbug will utilize the following environment variables if they exist: EDITOR Specifies the preferred editor. If EDITOR is not set, bashbug defaults to emacs. HOME Directory in which the failed bug report is saved if the mail fails. TMPDIR Directory in which to create temporary files and directories. SEE ALSO
bash(1) AUTHORS
Brian Fox, Free Software Foundation bfox@gnu.org Chet Ramey, Case Western Reserve University chet@po.cwru.edu GNU Bash-4.0 1998 July 30 BASHBUG(1)
All times are GMT -4. The time now is 02:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy