Register
It is currently Mon Apr 23, 2018 6:46 pm

[SOLVED]Extract characters 2 by 2


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Tue Jul 26, 2016 6:51 am   

Joined: Tue Jul 26, 2016 6:46 am
Posts: 5
Dear all
Suppose I have a string like "abcdef". How can I extract its characters 2 by 2 such that the output seems to be:

ab
bc
cd
de
ef


Last edited by David on Tue Jul 26, 2016 12:05 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Tue Jul 26, 2016 9:49 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
string=abcdefg
string_length=${#string}
echo \"$string\" is $string_length long.

if (( string_length % 2 > 0 )) ; then
   needed_steps=$((   string_length / 2   ))
else
   needed_steps=$(( string_length / 2 - 1 ))
fi
echo we do $needed_steps steps
for (( i=0; i <= needed_steps; i++ )); do     echo ${string:i*2:2}; done
A pair of two round braces (( some integer math here )) denote an arithmetical context. Within an arithmetical context there is no need to expand vars by prefixing a dollar sign.

The % (modulo) operator does an integer division and gives the remainder.
Hence the expression (( string_length % 2 > 0 )) reads like : If the remainder of integer division of string_length by 2 is greater than zero.

We need this discrimination to suppress output of blank lines.

If an arithmetic expression is prefixed by a dollar sign, bash expands the expression to its result which in this case is the number of steps needed.

We use the needed_steps to control a for loop.
Read about this loop with man bash | sed -rn '/for \(\( expr1/,/^$/p'

Within the for loop we use bash expansion echo ${string:i*2:2}
Read about this with man bash | sed -rn '/\$\{parameter:offset:length\}/,/\$\{!prefix/{/\$\{\!prefix/d;p}' | less
Note that the addressing within this Parameter Expansion is an arithmetic context as well.
So we can expand the var i without the dollar prefix. And bash indexes start always with zero.

Bash knows lots of Parameter Expansions.
Read about with man bash | sed -rn '/Parameter Expansion/,/Command Substitution/{/Command Substitution/d;p}' | less


Top
 Profile  
 PostPosted: Tue Jul 26, 2016 10:59 am   

Joined: Tue Jul 26, 2016 6:46 am
Posts: 5
Dear uhelp

Very thanks for your attention. There are many ideas in your answer for me (as a newbie). But the output is
Code:
"abcdefg" is 7 long.
we do 3 steps
ab
cd
ef
g

My target is
Code:
ab
bc
cd
de
ef
fg

So it is not important that the length of string is even or odd.


Top
 Profile  
 PostPosted: Tue Jul 26, 2016 11:40 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
string="abcdefg"
string_len=${#string}
for (( i=0; i < $string_len; i++)); do
   echo ${string:i:2}
done
With given infos from my first answer you could have written this yourself.


Top
 Profile  
 PostPosted: Tue Jul 26, 2016 12:04 pm   

Joined: Tue Jul 26, 2016 6:46 am
Posts: 5
Yes. I changed your
Code:
$string_len

into
Code:
$string_len - 1

Thanks again.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP