bash case statement output help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash case statement output help
# 1  
Old 02-08-2010
bash case statement output help

greetings,

I have a script that is taking input like this:

a
b
c
d
aa
bb
aaa
bbb
ccc
ddd

and formating it to be like this:
a b c d
aa bb
aaa bbb ccc ddd

however, the only way i can get this to work and not have the last line clipped off is putting the echo in the *) of the case statement which actually gives me output like this:

a
a b
a b c
a b c d
aa
aa bb
aaa
aaa bbb
aaa bbb ccc
aaa bbb ccc ddd

is there a) any way to not have to put the echo in the *) of the case statement and still get the 3 lines i need? or b) is there anything that i can use to just print out the longest line that starts with a/aa/aaa (please keep in mind i would like to be able to set the delimiter to be :'s)

If this is too confusing, let me know and i will post some sanitized output and code.

thanks!
# 2  
Old 02-08-2010
It seems to me you should put an echo statement after your while loop, not in the case statement. Otherwise please post what you have tried so far, otherwise it remains a bit of a stab in the dark.
# 3  
Old 02-08-2010
Gives weird cascading output
file output.txt:
Code:
a
b
c
d
aa
bb
aaa
bbb
ccc
ddd

attempts:
weird cascading output:
Code:
cat output.txt|while read LINE; do case "$LINE" in a*) unset FOO; FOO[0]="$LINE";; *) FOO[$[${#FOO[@]}+1]]="$LINE";; esac; echo ${FOO[*]}; done

and

Code:
cat output.txt|while read LINE; do case "$LINE" in a*) unset FOO;  FOO[0]="$LINE";; *) FOO[$[${#FOO[@]}+1]]="$LINE"; echo ${FOO[*]};; esac;  done

cuts off last line:
Code:
cat output.txt|while read LINE; do case "$LINE" in a*) echo ${FOO[*]}; unset FOO;  FOO[0]="$LINE";; *) FOO[$[${#FOO[@]}+1]]="$LINE";; esac;  done

gives nothing:
Code:
cat output.txt|while read LINE; do case "$LINE" in a*) unset FOO;  FOO[0]="$LINE";; *) FOO[$[${#FOO[@]}+1]]="$LINE";; esac;  done; echo ${FOO[*]}

# 4  
Old 02-08-2010
Hi, try:

Code:
while read LINE
do
  case "$LINE" in 
      a*) echo ${FOO[*]}
          unset FOO
          FOO[0]="$LINE";;
       *) FOO[$[${#FOO[@]}+1]]="$LINE";;
  esac
done < output.txt
echo ${FOO[*]}

-or-
Code:
while read LINE
do
  case "$LINE" in
      a*) printf "\n"
  esac
  printf "%s" "$LINE"
done < output.txt
printf "\n"


Last edited by Scrutinizer; 02-08-2010 at 01:56 PM..
# 5  
Old 02-08-2010
The first one worked really well. Thanks!!! one last question -- what if rather than a file, i needed to grab the input from stdin? (and i can't write it out to a file first)

thanks!

[edit]

I ended up making what you gave me into its own script, and then called that from my first script and it all worked perfectly! thanks again!!

Last edited by adambot; 02-08-2010 at 02:18 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

BASH - case statement

Hi Gurus, I have the below BASH code which does not works for upper case alphabets except Z (upper case Z). What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working. case $var in ) echo "Lower case alphabet" ;; ... (7 Replies)
Discussion started by: GaneshAnanth
7 Replies

3. UNIX for Dummies Questions & Answers

Case statement help

Hi I am new to shell scripting, I wanted to make a shell script that has a case statement asking the user to select their city 1)london 2)tokyo 3) etc., I then want the users input to be stored in a variable and echoed out in another script; so for example if the user selects tokyo, tokyo city code... (2 Replies)
Discussion started by: scriptnewbie
2 Replies

4. Shell Programming and Scripting

Bash case Statement and Using Line Anchors?

Hello All, I am writing a script that is to be placed on multiple servers, and of course I've started running into some compatibility issues for certain shell commands. The code below worked just fine on most of my machines except for a couple. Here I had 4 separate lines in my script that... (3 Replies)
Discussion started by: mrm5102
3 Replies

5. 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

6. Shell Programming and Scripting

Can you use logical operators in a case statement (bash)?

I'm pretty sure I already know the answer to this, but I want to make sure I'm not overlooking anything. I'm working on a log monitoring script and every 10 lines I want to display a summary of events. The thing is, there are a lot of possible events, that likely won't have happened, so I only want... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

7. Shell Programming and Scripting

[BASH] recognise new line regex in case statement

Hi, I'm trying to write a routine to parse a file that contains data that will be read into arrays. The file is composed of labels to identify data types and arbitrary lines of data with the usual remarks and empty new lines as is common with config files. The initial pass is built as so:... (3 Replies)
Discussion started by: ASGR
3 Replies

8. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

9. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

10. Shell Programming and Scripting

case statement

hi all i'm writing a script and in it i need to prompt the user if the entered value is correct or not ,i wrote the following and its not working ,its executing the script even if i enter Y/N pls any help is appreciated echo "\nAre you sure you entered the right Destination Environment? y :... (5 Replies)
Discussion started by: bkan77
5 Replies
Login or Register to Ask a Question