2017-05-12 1 views

Antwort

3

Mit BASH:

a="This is test.txt file" 
s="${a%%.*}"   # remove all text after DOT and store in variable s 
echo "$((${#s} + 1))" # get string length of $s + 1 

13 

Oder mit awk:

awk -F. '{print length($1)+1}' <<< "$a" 
13 
+1

Excellent. Kannst du eine kurze Erklärung von 2 Zeilen geben (s = "$ {a %%. *}") "? –

+0

sicher, ich habe eine Erklärung in der Antwort hinzugefügt – anubhava

1

Verwendung von C:

#include <stdio.h> 
#include <string.h> 

int main() { 

    char a[] = "This is test.txt file"; 

    int i = 0; 
    while(i < strlen(a)) { 
      if(a[i] == '.') { 
        printf("%d", i + 1); 
        break; 
      } 
      i++; 
    } 

    return 0; 
}