Solved

Awk substr query

Posted on 2003-10-21
3
30,152 Views
Last Modified: 2013-12-26
Hello,
I'm a newbie so forgive me if I am doing something silly. I have a string passed into my shell script as an argument. I only want every other character of this string.

while [ $num -le $inputlength ]
do
  echo $num
  echo $inputpass| awk '{print substr($0,$num,1)}'
  let num=$num+2
done

The thing is, the substr function does not seem to recognise the $num variable. The only thing that is displayed is the first character of the input string. Is there something I need to do to $num so that the substr procedure can see it?

TIA

John
0
Comment
Question by:johnster_uk
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
glassd earned 125 total points
Comment Utility
Why not do the whole thing in awk. Assuming you want all the characters on one line:

echo $inputpass | awk '{
   for(i=1;i<=length($0);i+=2) {
      printf("%s",substr($0,i,1))
   }
   printf("\n")
}'

You can do something like this in ksh:

Length=${#inputpass}
i=1
while (( $i <= $Length ))
do
   Char=$(echo $inputpass | cut -c$i)
   printf "%s" $Char
   (( i = i + 2 ))
done
print "\n"

But if you really want to do it your way, why not do this:

while [ $num -le $inputlength ]
do
  echo "$inputpass $num" | awk '{print substr($1,$2,1)}'
  let num=$num+2
done

Note the alternative addition syntaxes available

You cannot see your shell variables inside awk, so you have to pass them in. There are a few ways. One is to include them on the input line as shown above (this assumes that $inputpass cannot include spaces or tabs). Another is to set them to a variable, like this:

echo $inputpass | awk -v a=$num '{print substr($0,$num,1)}'

Hope this helps.



0
 
LVL 6

Expert Comment

by:JJSmith
Comment Utility

Whilst there is nothing wrong with the good answer(s) supplied by glassd, it depends on the percieved question. ie.
        "Is there something I need to do to $num so that the substr procedure can see it?"

Well yes there is, $num is a shell variable, awk has it's own set of variables, as demonstated by glassd you can assign the shell variable to an awk variable as you enter the awk process.

        echo $inputpass | awk -v awkvar=$num '{print substr($0,awkvar,1)}'

Because of the 2 variable types appearing on the same command line, we protect the awk variables , ( the $ prefixed ones ), by enclosing the awk commands in single quotes to stop the shell trying to evaluate the $ variables. In your case you wanted the shell to evaluate $num before the awk command line is executed.

In short your original code was OK except you didn't want to protect the $num.

ie
echo $inputpass| awk '{print substr($0,$num,1)}'
becomes
echo $inputpass| awk '{print substr($0,'$num',1)}'

Try it

Cheers
JJ







0
 
LVL 7

Expert Comment

by:glassd
Comment Utility
Would you believe it. All the time I have been using awk and I didn't know you could do that. Just shows, you're never too old to learn.

Thanks JJ
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 78
viewing source code from eclipse 13 74
modThree challenge 4 64
haveThree challenge 22 100
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now