testing the last character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting testing the last character
# 1  
Old 06-04-2009
testing the last character

hi

i try to test the last character in a variable (here $i )

assume i=kljlkjlkA it should be KO and lkjljjlT KO

if [ "echo $i|grep A$" ]
then
echo "ending with A"
else
echo "no A at the end"
fi

whether i is ending or not with A i got "no A at the end"

i tried with simple [ double this is the same , i think the pb is with the Interpretation of A$

thanks in advance
Christian
# 2  
Old 06-04-2009
It's not that simple, I daresay - but possible, of course:

Code:
#! /bin/bash

INPUT="$1"
LENGTH=${#INPUT}
if [ "${INPUT:$(($LENGTH-1)):1}" = "O" ]
then
  echo "good"
else
  echo "bad"
fi

exit 0

Code:
[house@leonov] sh test.bash 'kljlkjlkA it should be KO and lkjljjlT KO'
good

# 3  
Old 06-04-2009
Here are two ways of doing it. Both ways work in bash and ksh93
Code:
i=kljlkjlkA

if [[ ${i: -1} == "A" ]]
then
    echo "ending with A"
else
    echo "no A at the end"
fi

if [[ $i =~ A$ ]]
then
    echo "ending with A"
else
    echo "no A at the end"
fi

# 4  
Old 06-04-2009
thanks but it doesn't work for me :

# sh test.bash 'kljlkjlkA it should be KO and lkjljjlT KO'
test.bash[6]: "${INPUT:$(($LENGTH-1)):1}": bad substitution

i'm in ksh but in bash it's the same !

regards
Christian

-----Post Update-----

Thanks to help me :

i=kljlkjlkA

if [[ ${i: -1} == "A" ]]
then
echo "ending with A"
else
echo "no A at the end"
fi

=> i got :

# test.ksh
test.ksh: ${i: -1}: bad substitution

if [[ $i =~ A$ ]]
then
echo "ending with A"
else
echo "no A at the end"
fi

==> i got :
test.ksh
test.ksh: syntax error at line 1 : `=~' unexpected

regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

3. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

4. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

5. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

6. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

7. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

10. Shell Programming and Scripting

Testing the last character in a string

Hi In the shell scripted I'm trying to write! I would like to test the last character in a string. The string is a path/directory and I want to see if the last character is a '/'. The string (path/directory) is inputted by a user. If the '/' character isn't present then I want to be able to... (11 Replies)
Discussion started by: dbrundrett
11 Replies
Login or Register to Ask a Question