We help IT Professionals succeed at work.
Get Started

Get the matching string prefix

tommym121 asked
Last Modified: 2013-12-26
Hi, I would like to exact the first X character of a string S1 where X is the lenght of a string S2. How to do that in shell ksh script

Here is my testing script

EEE=`echo $OUT_DATA | awk -F "ABC" '{printf("%s", $1)}'`
FFF=`echo $OUT_DATA | awk "ABC" '{printf("%s", $1)}'`

echo "EEE = $EEE"
echo "FFF = $FFF"

DDD=$(echo $OUT_DATA |awk '{print substr($0, 0 , 7))}')

Both EEE and FFF returns empty string.
DDD is okay, I am getting ABCDEFG back
But, how do i make it more dynamic? That is, somehow the length field in substr can
be determined based on the length of $MMM
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE