Problem with case statement

Hi,

I need modify the all lines in a file into one format.

cat file

htec.twe34c.ATI
.hesh.twdghu..ATI
..hesh.twdghu..ATI
htec.twe3
hjsct14567ati

Output should have 16 characters

htectwe34c   ATI
heshtwdghu   ATI
heshtwdghu   ATI
htectwe3     ATI
hjsct14567   ATI

This is my code:

#!/bin/ksh
echo "enter the file name"
read file
sed -e 's/\.//g' -e 's/ //g' $file>file1
y=ATI
while read line
do
n=`echo $line|wc -c`
echo $n
new=`echo $line |sed 's/\(.*\).../\1/'`
x=`echo $new|wc -c`
echo $x
var=`echo $line|awk '{print substr($0, length($0)-2)}'`
if [ "$var" != "$y" ] 
then
echo $line
case $n in
6) `echo $line|sed -e 's/$/       ATI/'>>finalresult` ;;
7) `echo $line|sed -e 's/$/      ATI/'>>finalresult` ;;
8) `echo $line|sed -e 's/$/     ATI/'>>finalresult` ;;
9) `echo $line|sed -e 's/$/    ATI/'>>finalresult` ;;
10) `echo $line|sed -e 's/$/   ATI/'>>finalresult` ;;
11) `echo $line|sed -e 's/$/  ATI/'>>finalresult` ;;
12) `echo $line|sed -e 's/$/ ATI/'>>finalresult` ;;
13) `echo $line|sed -e 's/$/ATI/'>>finalresult` ;;
*) echo "bye" ;;
esac
else
echo $new
case $x in
6) `echo $new|sed -e 's/$/       ATI/'>>finalresult` ;;
7) `echo $new|sed -e 's/$/      ATI/'>>finalresult` ;;
8) `echo $new|sed -e 's/$/     ATI/'>>finalresult` ;;
9) `echo $new|sed -e 's/$/    ATI/'>>finalresult` ;;
10) `echo $new|sed -e 's/$/   ATI/'>>finalresult` ;;
11) `echo $new|sed -e 's/$/  ATI/'>>finalresult` ;;
12) `echo $new|sed -e 's/$/ ATI/'>>finalresult` ;;
13) `echo $new|sed -e 's/$/ATI/'>>finalresult` ;; 
*) echo "bye" ;;
esac
fi
done < file1 

1) case is not working. its not redirecting output to "finalresult" file.
2) but every time it is executing default condition i.e bye
3) WC -C command is giving wrong count.
ex: echo "unix"|wc -c
o/p :5 (instead of 4)

I am using ksh...can any one help me out.

instant help is really required
Thanks a lot

Kartheek,
For your 3rd query, the output would be 5 as wc -c gives the no of bytes and it does include the new line character as well.

Also, if you have the same command for execution in the case statement then, you can put 6|7|8|9|10|11|12|13) followed by the statements instead of writing each one as a separate case.

Regards,
Shantanu

---------- Post updated at 03:42 PM ---------- Previous update was at 03:12 PM ----------

Kartheek,
The script seems to be working fine. The only time it went into the default was for the last line when the variable "var" and "y" don't match and the value of n is 14. See the input and output below,

$ sh test.sh
enter the file name
file
14
11
htectwe34c
14
11
heshtwdghu
14
11
heshtwdghu
9
6
htectwe3
14
11
hjsct14567ati
bye

$ cat file
htec.twe34c.ATI
.hesh.twdghu..ATI
..hesh.twdghu..ATI
htec.twe3
hjsct14567ati

$ cat file1
htectwe34cATI
heshtwdghuATI
heshtwdghuATI
htectwe3
hjsct14567ati

$ cat finalresult
htectwe34c ATI
heshtwdghu ATI
heshtwdghu ATI
htectwe3 ATI
htectwe34c ATI
heshtwdghu ATI
heshtwdghu ATI
htectwe3 ATI

Instead of those unnecessary case statements, how about:

#!/bin/ksh
echo "enter the file name"
read file
y=ATI
while read line
do
  line=${line//.}                # remove dots
  line=${line%[aA][tT][iI]}      # remove last three characters if ATI or ati
  if [[ ${#line} -ge 6 && ${#line} -le 13 ]]; then
    printf "%-12s %s\n" $line $y
  fi
done<"$file">finalresult

result:

htectwe34c   ATI
heshtwdghu   ATI
heshtwdghu   ATI
htectwe3     ATI
hjsct14567   ATI

hi,

Still i am facing problem with case.
for all x and n values it is executing default condition .
finally output file filled with only "bye" :frowning: :frowning:

sed 's/[aA][tT][iI]$//;s/\.//g;s/$/\tATI/' urfile
htectwe34c      ATI
heshtwdghu      ATI
heshtwdghu      ATI
htectwe3        ATI
hjsct14567      ATI