Sponsored Content
Top Forums Shell Programming and Scripting To find nth position of character in string Post 302901423 by Chubler_XL on Tuesday 13th of May 2014 05:29:17 PM
Old 05-13-2014
You could try this function:

Code:
function index
{
    p=1
    f=0
    t="$1"
    catch=""
    while [ $p -le ${#1} ]
    do
        if [ "${t#$2}" != "$t" ]
        then
            let f=f+1
            if [ $f -eq ${3:-1} ]
            then
                echo $p
                return
            fi
        fi
        t="${1#$catch?}"
        catch="${1%$t}"
        let p=p+1
    done
    echo 0
}

Call it like this:

Code:
index "UK,TK,HK,IND,AUS" , 4
# Result: 13

Or even this:

Code:
index "UK,TK,HK,IND,AUS" "?K" 3
# Result: 7


Last edited by Chubler_XL; 05-13-2014 at 06:56 PM.. Reason: Fixup variables not reset from previous calls
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I get the nth character from a string?

How to I get the nth character from a string in shell script. For instance, I have a string London. I want to get, say the first character (L) from the string. How do I do this in unix shell? Thankx (4 Replies)
Discussion started by: toughman
4 Replies

2. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies

3. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (2 Replies)
Discussion started by: manaswinig
2 Replies

4. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies

5. Shell Programming and Scripting

Replace character in certain position in a string

Hello everyone this is my first post of many to come :) I am writing a script and in this script at one point i need to replace a character in a particular position in a string for example: in the string "mystery" i would need to replace the 3rd position to an "r" so the string becomes... (3 Replies)
Discussion started by: snipaa
3 Replies

6. Shell Programming and Scripting

How to find character position in file?

how to find character positionin file? i.e string = "123X568" i want to find the position of character "X". Thanks (6 Replies)
Discussion started by: LiorAmitai
6 Replies

7. Shell Programming and Scripting

How to extract the certain position's character in a string

Suppose there are two files: A, format is like: line1 12 line2 33 line3 6 ... B, format is like: >header taaccctaaccctaaccctaacccaaccccaccccaaccccaaccccaac ccaaccctaaccctaaccctaacccaaccctaaccctaaccctaacccaa ccctcaccctcaccctcaccctcaccctcaccctcaccctcaccctaacc... (1 Reply)
Discussion started by: bioinflix
1 Replies

8. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

9. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

10. Shell Programming and Scripting

Assign a variable the nth character of a string.

I see a millioin ways to do this with echo, but what I wan to do is assign a variable the "nth" character of an incoming parameter to a ksh script. $1 will be "pia" I need to assign the first character to stmttype. (10 Replies)
Discussion started by: klarue
10 Replies
telecode(5)							File Formats Manual						       telecode(5)

NAME
telecode - A character encoding system (codeset) for Traditional Chinese DESCRIPTION
The Telecode codeset (called Mitac Telex in early versions of the operating system) consists of 2 character planes. Each character plane has 8836 character positions. In plane 1, standard characters occupy positions 0001 to 8045; the remaining 791 positions are for user- defined characters. In plane 2, standard characters occupy positions 0001 to 8489; the remaining 346 positions are for user-defined charac- ters. Telecode uses 2-byte values to represent characters on both planes. Plane 1 Character Encoding To differentiate plane 1 code from plane 2 code, the most significant bit (MSB) is set on in both bytes of a plane 1 character code. The following formula calculates the value of a plane 1 character from its position on the plane: 1st byte = M + 161 2nd byte = N + 161 - M x 94 In this formula, N is the position of the character and M = N / 94. For example, if a character is at position 2502 on plane 1, its encoding value is BBDB, which is calculated as follows: N = 2502, M = 2502/94 = 26 1st byte = 26 + 161 = 187 2nd byte = 2502 + 161 - 26 x 94 = 219 Plane 2 Character Encoding To differentiate plane 2 code from plane 1 code, the MSB of the first byte is set on and that of the second byte is set off for each plane 2 character code. The following formula calculates the value of a plane 2 character from its position: 1st byte = M + 161 2nd byte = N + 33 - M x 94 In this formula, N is the position of the character on the plane and M = N / 94. For example, if a character is at position 2502 on plane 2, its encoding value is BB5B, which is calculated as follows: N = 2502, M = 2502/94 = 26 1st byte = 26 + 161 = 187 2nd byte = 2502 + 33 - 26 x 94 = 91 Codeset Conversion The following codeset converter pairs are available for converting Traditional Chinese characters between telecode and other encoding for- mats. Refer to iconv_intro(5) for an introduction to codeset conversion. For more information about the other codeset for which telecode is the input or output, see the reference page specified in the list item. big5_telecode, telecode_big5 Converting from and to the Big-5 codeset: big5(5). Note that Big-5 encoding is equivalent to the Microsoft code-page format used on PCs for Traditional Chinese. You can therefore use these converters to convert Traditional Chinese characters between PC code page format and Telecode encoding format. For more infor- mation on how the operating system supports PC code pages, see code_page(5). dechanyu_telecode, telecode_dechanyu Converting from and to the DEC Hanyu codeset: dechanyu(5). eucTW_telecode, telecode_eucTW Converting from and to Taiwanese Extended UNIX Code: eucTW(5). Font Support for Telecode The operating system supports Telecode only through conversion to another codeset. SEE ALSO
Commands: locale(1) Others: ascii(5), big5(5), Chinese(5), code_page(5), dechanyu(5), dechanzi(5), eucTW(5), GBK(5), i18n_intro(5), i18n_printing(5), iconv_intro(5), l10n_intro(5), sbig5(5) telecode(5)
All times are GMT -4. The time now is 08:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy