Problem with case statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with case statement
# 1  
Old 12-02-2009
Problem with case statement

Hi,

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

Code:
cat file

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

Output should have 16 characters

Code:
htectwe34c   ATI
heshtwdghu   ATI
heshtwdghu   ATI
htectwe3     ATI
hjsct14567   ATI

This is my code:
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

Last edited by pludi; 12-02-2009 at 06:56 AM.. Reason: code tags, please...
# 2  
Old 12-02-2009
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,

Code:
$ 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


Last edited by pludi; 12-02-2009 at 06:56 AM.. Reason: code tags, please...
# 3  
Old 12-02-2009
Instead of those unnecessary case statements, how about:
Code:
#!/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:
Code:
htectwe34c   ATI
heshtwdghu   ATI
heshtwdghu   ATI
htectwe3     ATI
hjsct14567   ATI


Last edited by Scrutinizer; 12-02-2009 at 10:02 AM..
# 4  
Old 12-02-2009
problem with case statement

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" Smilie Smilie
# 5  
Old 12-02-2009
Code:
sed 's/[aA][tT][iI]$//;s/\.//g;s/$/\tATI/' urfile
htectwe34c      ATI
heshtwdghu      ATI
heshtwdghu      ATI
htectwe3        ATI
hjsct14567      ATI

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Strange Problem in case statement while shift

Hi, Here is my code as below: test.ksh: ======= #!/bin/ksh option="${1}" while do case $1 in -f) FILE="${2}" echo "File name is $FILE" ;; -d) DIR="${2}" echo "Dir name is $DIR" ;; -*) echo "`basename ${0}`:usage: | " (5 Replies)
Discussion started by: zaq1xsw2
5 Replies

2. Homework & Coursework Questions

Case Statement

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hey, guys I really need some help with a project. "Write a shell program that examines the command line... (8 Replies)
Discussion started by: sk192010`
8 Replies

3. Shell Programming and Scripting

Problem using bash case statement

I have the following bash script and it is not accepting the lines "--"|"--""-") "--""-"") while do echo "Current Argument is ${1}" case "$1" in "--"|"--""-") echo "Argument is ${1}" shift # Skip ahead one to the next argument. ... (1 Reply)
Discussion started by: kristinu
1 Replies

4. Homework & Coursework Questions

Problem with executing a possible if or case statement script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a phonebook program. It should use functions to perform the required tasks. It should be menu-based,... (1 Reply)
Discussion started by: Rgasin02
1 Replies

5. Shell Programming and Scripting

Problem using 'Case' statement

Hi, When I execute the below script, I am getting the error as ' is not expected.ror at line 3 : `in #!/bin/sh case $1 in -r) echo Force deletion without confirmation ;; -i) echo Confirm before deleting ;; *) echo Unknown argument ;; esac I could not see any problem with... (1 Reply)
Discussion started by: Sathish_Obla
1 Replies

6. Shell Programming and Scripting

Problem with CASE statement

Guys, Here is the script syntax which is not accepting the parameters & not performing the said activity. $ ./routing.sh xyz123-ra str enable ********************************************************************** Preparing to service the request for Device xyz123-ra in Question... (9 Replies)
Discussion started by: raghunsi
9 Replies

7. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

8. Shell Programming and Scripting

what is problem with this small shell script.. case statement related

Hi All, this small script is written to recognize user input character.. it is in small case .. upeer case or is a number... but when i input first capital letter say A.. it always gives small character.... what is the problem. #!/bin/bash echo "Enter the character" read a case $a in )... (2 Replies)
Discussion started by: johnray31
2 Replies

9. UNIX for Advanced & Expert Users

Case statement problem

I need to display this menu and accept variables. Can someone tell me why i am having a problem with this case statement, please # TAPE MANAGER MAIN MENU tapemgr_Main_Menu() { echo "Legato Tape Management System Menu" echo " This system is used to report Legato ERV Offsite and Tapes... (6 Replies)
Discussion started by: gzs553
6 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question