Conditional for every letter in alphabet


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional for every letter in alphabet
# 1  
Old 11-12-2014
Conditional for every letter in alphabet

I wanted to know if there was a more efficient to do this. I was to setup a conditional for every letter of the alphabet, like so (I am parsing an array):
Code:
for i in "${arr[@]}"; do
if [[ $i == A* ]]; then
	echo "$i starts with A"
	else echo "$i does not start with A"
fi
done

I want to do this A-Z, is there a better way other than duplicating the if statement 26 times?
# 2  
Old 11-12-2014
Using case statement?
This User Gave Thanks to vbe For This Post:
# 3  
Old 11-12-2014
Agreed, case is a better choice.
# 4  
Old 11-12-2014
Code:
for i in "${arr[@]}"; do
if [[ $i =~ [A-Za-z].* ]]; then
j=$i
while [[ $j =~ ...* ]]
do
 j=${j%?}
done
 echo "$i starts with $j"
	else echo "$i does not start with a letter"
fi
done

Check for a letter using regex matching in bash, the find the first character for your output.
# 5  
Old 11-12-2014
If I understand right, the solution might be to put a for loop in a for loop (sup dawg Smilie):
Code:
for i in "${arr[@]}"; do
 for j in {A..Z}; do
  if [[ $i == $j* ]]; then
    echo "$i starts with $j"
    else echo "$i does not start with $j"
  fi
 done
done

# 6  
Old 11-12-2014
That needs an enhancement for no 'does not start' messages for other letters, I bet.

Cleaner:
Code:
for i in "${arr[@]}"; do
 if [[ $i =~ [A-Za-z].* ]]; then
  j=${i:0:1}
  echo "$i starts with $j"
 else
  echo "$i does not start with a letter"
 fi
done

I seem to live in ksh systems, and can't afford to get too bash'y when doing ksh prod support and scripts.

Last edited by DGPickett; 11-12-2014 at 05:49 PM..
# 7  
Old 11-13-2014
What about
Code:
LETTERS=$(echo {A..Z})
W=Caesar
echo $W doesn\'t start with ${LETTERS/${W:0:1}}
Caesar doesn't start with A B D E F G H I J K L M N O P Q R S T U V W X Y Z

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script - Alphabet in code

Hi e Hi everyone, I can't make this script work, #! /bin/bash declare -A crypt=( ="A" ="a" ="B" ="b" ="C" ="c" =' ' ='!' ) encode () { local word=$1 for ((i=0; i<${#word}; ++i)) ; do local char=${word:$i:1} printf %s' ' ${crypt} done ... (5 Replies)
Discussion started by: Pinguino
5 Replies

2. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

3. Shell Programming and Scripting

Help with sort alphabet on specific column

Input file: POL B7U6K8 Avian_reticuloendotheliosis_virus POLB B7Z1W5 Homo_sapiens POLB H9G5Y0 Anolis_carolinensis POLD1 Q642R8 Xenopus_laevis POLD2 H0YZC7 Taeniopygia_guttata POLD3 F1P540 Gallus_gallus POLDIP3 Q5F4B6 Gallus_gallus POLE2 E1C2T8 Gallus_gallus... (3 Replies)
Discussion started by: perl_beginner
3 Replies

4. Shell Programming and Scripting

Recode alphabet into numbers

I have a genotype.bim file where it contains information about SNPs and genotype. As a hypothetical example, let's say genotype.bim snp1 ... A G snp2 ... G T snp3 ... G T snp4 ... G A ... snpN ... C G where first column identifies each SNP and 5th and 6th column has genotype... (3 Replies)
Discussion started by: johnkim0806
3 Replies

5. Shell Programming and Scripting

last character is digit or alphabet!

Hello, I have to find out whether the last character is digit or alphabet. I manage to strip the last character but would need some help if there is one liner available to test the above. set x = WM echo $x | sed 's/.*\(.$\)/\1/' O/P M I would like a one liner code to test whether the... (1 Reply)
Discussion started by: dixits
1 Replies

6. Shell Programming and Scripting

Alphabet counting

I have a text file in the following format CCCCCGCCCCCCCCCCcCCCCCCCCCCCCCCC AAAATAAAAAAAAAAAaAAAAAAAAAAAAAAA TGTTTTTTTTTTTTGGtTTTTTTTTTTTTTTT TTTT-TTTTTTTTTCTtTTTTTTTTTTTTTTT Each row/line will have 32 letters and each line will only have multiple occurrences of 2 letters out of a pool... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

7. UNIX for Dummies Questions & Answers

checking wether an input is using letters of the alphabet

afternoon forums. I need to get a way of testing as to wether an inputed character is part of the english alphabet. i have come up with the following code but its not working at all. until '] do echo This is not a Letter done any help would be beneficial to me. (1 Reply)
Discussion started by: strasner
1 Replies

8. Shell Programming and Scripting

To check if the first character is a alphabet or number

Hi, I need to find whether the first character in a line is a alphabet or a number. If its a number i should sort it numerically. If its a alphabet i should sort it based on the ASCII value.And if it is something other than alphabet or number then sort it based on ASCII value. The code i used... (2 Replies)
Discussion started by: ragavhere
2 Replies

9. Programming

output the letters of the alphabet with the number of occurrences

hi, I'm trying to create a program that will read a file and then check the file for each letter of the alphabet and then output the letter and the number of times it appears in the file, into a new file... this is what i have so far but it's not working.. if anyone could help that would be nice!... (10 Replies)
Discussion started by: svd
10 Replies

10. Shell Programming and Scripting

What can i do to check that the input is all alphabet.. ?

What can i do to check that the input is all alphabet.. ? (4 Replies)
Discussion started by: XXXXXXXXXX
4 Replies
Login or Register to Ask a Question

Featured Tech Videos