Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a string that is always 3 characters... the first one and the last one are always the same.

example:

▁▅█

Is there a simple way to only display the one in the middle? (which is the only one that changes)

share|improve this question
add comment

4 Answers 4

up vote 7 down vote accepted

Yes, try doing this, and pick your preferred method =) :

With :

echo "ixi" | grep -oP "^.\K."

With :

echo "ixi" | cut -c2

With parameter expansion :

x='ixi'; echo ${x:1:1}

With :

echo "ixi" | sed 's/.\(.\)./\1/'

or

echo "ixi" | sed 's/\(^.\|.$\)//g'

With :

echo "ixi" | perl -lne 'print $& if /^.\K./'

With :

echo "ixi" | ruby -ne 'print $_.split(//)[1]'

With :

echo 'ixi' | awk '{split($0, a, ""); print a[2]}'

With :

echo "ixi" | python -c 'print list("'$(cat)'")[1]'

or

python -c 'import sys; print list(sys.argv[1])[1]' ixi

NOTE

  • \K restart the match to zero (see pcre doc)
  • $(cat) in python is a shell hack to get STDIN
share|improve this answer
3  
+1 for the nice use of grep. –  Dave Jarvis Nov 15 '12 at 17:11
3  
Added the sortest one : cut –  sputnick Nov 15 '12 at 17:36
    
loved that grep –  Mr.Gando Nov 16 '12 at 4:35
add comment

You could also use sed instead of grep:

sed 's/.\(.\)./\1/'

This says:

  1. s/ ... match the expression up to the next / character.
  2. . ... match the first character (any).
  3. \(.\) ... match the next character and remember it.
  4. . ... match the third character (any).
  5. / ... denotes the end of the expression.
  6. \1 ... replace the entire string that matched with the character that was remembered.
  7. / ... end of replacement text.

Thus:

$ echo "abc" | sed 's/.\(.\)./\1/'

Will print:

b
share|improve this answer
add comment

        ︙
or

echo "ixi" | awk '{print substr($0,2,1)}'

or

expr "ixi" : '.\(.\).'
share|improve this answer
add comment

For variety, here's one way with dd:

echo ixi | dd bs=1 skip=1 count=1 2>/dev/null

If your grep doesn't support perl (-P), you could do it like this:

echo ixi | grep -o . | sed -n 2p

These alternatives only work for one-line input.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.