Skip to main content
added 437 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 108.2k
  • 21
  • 295
  • 520

shell function

A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those we want to a new variable

Here's that whole idea formatted into a nice function

crop_string_ends() { STR="$1" NEWSTR="" COUNT=0 while read -n 1 CHAR do COUNT=$(($COUNT+1)) if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ] then continue fi NEWSTR="$NEWSTR"$CHAR done <<<"$STR" echo $NEWSTR } 

And here is that same function in action:

$> crop_string_ends "|abcdefg|" abcdefg $> crop_string_ends "HelloWorld" elloWorl 

By the way, python does that much simpler:

Python

>>> mystring="|abcdefg|" >>> print(mystring[1:-1]) abcdefg 

or on command line:

$ python -c 'import sys;print sys.stdin.read()[1:-2]' <<< "|abcdefg|" abcdefg 

AWK

$ echo "|abcdefg|" | awk '{print substr($0,2,length($0)-2)}' abcdefg 

Ruby

$ ruby -ne 'print $_.split("|")[1]' <<< "|abcdefg|"  abcdefg 

A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those we want to a new variable

Here's that whole idea formatted into a nice function

crop_string_ends() { STR="$1" NEWSTR="" COUNT=0 while read -n 1 CHAR do COUNT=$(($COUNT+1)) if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ] then continue fi NEWSTR="$NEWSTR"$CHAR done <<<"$STR" echo $NEWSTR } 

And here is that same function in action:

$> crop_string_ends "|abcdefg|" abcdefg $> crop_string_ends "HelloWorld" elloWorl 

By the way, python does that much simpler:

>>> mystring="|abcdefg|" >>> print(mystring[1:-1]) abcdefg 

shell function

A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those we want to a new variable

Here's that whole idea formatted into a nice function

crop_string_ends() { STR="$1" NEWSTR="" COUNT=0 while read -n 1 CHAR do COUNT=$(($COUNT+1)) if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ] then continue fi NEWSTR="$NEWSTR"$CHAR done <<<"$STR" echo $NEWSTR } 

And here is that same function in action:

$> crop_string_ends "|abcdefg|" abcdefg $> crop_string_ends "HelloWorld" elloWorl 

Python

>>> mystring="|abcdefg|" >>> print(mystring[1:-1]) abcdefg 

or on command line:

$ python -c 'import sys;print sys.stdin.read()[1:-2]' <<< "|abcdefg|" abcdefg 

AWK

$ echo "|abcdefg|" | awk '{print substr($0,2,length($0)-2)}' abcdefg 

Ruby

$ ruby -ne 'print $_.split("|")[1]' <<< "|abcdefg|"  abcdefg 
Source Link
Sergiy Kolodyazhnyy
  • 108.2k
  • 21
  • 295
  • 520

A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those we want to a new variable

Here's that whole idea formatted into a nice function

crop_string_ends() { STR="$1" NEWSTR="" COUNT=0 while read -n 1 CHAR do COUNT=$(($COUNT+1)) if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ] then continue fi NEWSTR="$NEWSTR"$CHAR done <<<"$STR" echo $NEWSTR } 

And here is that same function in action:

$> crop_string_ends "|abcdefg|" abcdefg $> crop_string_ends "HelloWorld" elloWorl 

By the way, python does that much simpler:

>>> mystring="|abcdefg|" >>> print(mystring[1:-1]) abcdefg