Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with parsing a line

Status
Not open for further replies.

jaburke

Programmer
May 20, 2002
156
US
Hi,
I am having to rewrite a script from dtksh to ksh. I had something in the dtksh that looks like this:

if [[ $countit = 3 ]] then
var1=${line:0:10}
var2=${line:11:29}
var3=${line:42:1}
fi

This syntax for the line parsing doesn't translate to ksh. Can someone tell me an easy way to do this inside my ksh using sed or awk? I just want to read in one line from a file and parse it out. Any help would be appreciated. Thanks!
 
What about this ?
Code:
if [[ $countit = 3 ]] then
  var1=${expr substr "$line" 1 10}
  var2=${expr substr "$line" 12 29}
  var3=${expr substr "$line" 43 1}
fi

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I tried it and I get a "bad substitution" error.
 
Corrected some typos in PHV's solution (squiggly brackets):

Code:
if [[ $countit = 3 ]] then
  var1=$(expr substr "$line" 1 10)
  var2=$(expr substr "$line" 12 29)
  var3=$(expr substr "$line" 43 1)
fi

And some other ways to skin that cat:

Code:
if [[ $countit = 3 ]] then
  var1=$(echo "$line" | cut -c 1-10)
  var2=$(echo "$line" | cut -c 12-40)
  var3=$(echo "$line" | cut -c 43-43)
fi

Code:
if [[ $countit = 3 ]] then
  var1=$(echo "$line" | awk '{print substr($0,1,10)}')
  var2=$(echo "$line" | awk '{print substr($0,12,29)}')
  var3=$(echo "$line" | awk '{print substr($0,43,1)}')
fi

Strangely, I always thought that [[ ... ]] required a new line or semi-colon before then... but it seems to be only [ ... ] that does.

Also even more strangely, look at this HP-UX weirdness with expr substr and strings of numbers... any idea why this is happening?

Code:
# doesn't like numerics!
$ expr substr 12345678901234567890123456789012345678901234567890 3 10
34729262

# works fine with alphas?
$ expr substr abcdefghijklmnopqrstuvwxyz789012345678901234567890 3 10
cdefghijkl

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Using ksh93

Code:
# cat a
#!/usr/bin/ksh93

line="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
integer countit=3

if (( countit == 3 ))
then
        var1=${line:0:10}
        var2=${line:11:29}
        var3=${line:42:1}
fi

print ${var1}
print ${var2}
print ${var3}

# ./a
abcdefghij
lmnopqrstuvwxyz0123456789ABCD
G

Using ksh88
Code:
# cat a
#!/usr/bin/ksh

line="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
integer countit=3

if (( countit == 3 ))
then
        typeset -L10 var1=$line
        typeset -L40 var2=$line
        typeset -R29 var2=$var2
        typeset -L43 var3=$line
        typeset -R1 var3=$var3
fi

print ${var1}
print ${var2}
print ${var3}

# ./a
abcdefghij
lmnopqrstuvwxyz0123456789ABCD
G
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top