Sponsored Content
Top Forums Shell Programming and Scripting What is wrong with my if loop? Post 302913505 by miriammiriam on Monday 18th of August 2014 03:23:41 AM
Old 08-18-2014
What is wrong with my if loop?

Hello everyone,

I need a little help. I wrote a cshell script to change the format of a file but it is not working.

input file is like that:

Code:
2014  3 20 15  0     5.270 40.7739  27.6471  20.232   0.6  0  0  0      1
Site6    4.081  1.00 P
Site6    7.585  1.00 S
Site1    4.441  1.00 P
Site1    6.922  1.00 S
Site7    4.743  1.00 P
Site7    8.710  1.00 S
2014  3 20 15  3    33.491 40.9253  27.6682  22.619   1.2  0  0  0      1
Site1    4.483  1.00 P
Site1    7.798  1.00 S
Site6    9.443  1.00 S
Site7   10.101  1.00 S
2014  3 20 15 30    46.430 40.8773  27.8578   6.660   9.9  0  0  0      1
Site7    5.938  1.00 S
Site1    6.202  1.00 S
Site6    7.728  1.00 S

and I want the output file like that:

Code:
2014  3 20 15  0     5.270 40.7739  27.6471  20.232   0.6  0  0  0      1
Site6P    4.081  1.00
Site6S    7.585  1.00
Site1P    4.441  1.00 
Site1S    6.922  1.00 
Site7P    4.743  1.00 
Site7S    8.710  1.00 
2014  3 20 15  3    33.491 40.9253  27.6682  22.619   1.2  0  0  0      1
Site1P    4.483  1.00 
Site1S    7.798  1.00 
Site6S    9.443  1.00 
Site7S   10.101  1.00 
2014  3 20 15 30    46.430 40.8773  27.8578   6.660   9.9  0  0  0      1
Site7S    5.938  1.00 
Site1S    6.202  1.00 
Site6S    7.728  1.00

I use csh to change the format and my if loop is that:
Code:
foreach line ("`cat pha.dat`")
set argv = ( $line )
set name1 = $1

if $name1 == 2014 then
awk '{print $0}' pha.dat > dnm

else

awk '{ print $1, $4, $2, $3}' pha.dat > dnm

endif

end

the output is:

Code:
2014 3 0 20 15
Site6 P 4.081 1.00
Site6 S 7.585 1.00
Site1 P 4.441 1.00
Site1 S 6.922 1.00
Site7 P 4.743 1.00
Site7 S 8.710 1.00
2014 3 3 20 15
Site1 P 4.483 1.00
Site1 S 7.798 1.00
Site6 S 9.443 1.00
Site7 S 10.101 1.00
2014 3 30 20 15
Site7 S 5.938 1.00
Site1 S 6.202 1.00
Site6 S 7.728 1.00

which is not what I want. The loop is not working? Where am I making mistake?

Thank you!!!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

new to shell scripting: whats wrong with my if loop

#!/bin/bash for file in $HOME/*; do if ; then rm -i $file > /dev/null echo "$?" echo "$file has been deleted" fi done Been trying to learn shell scripting for a week or so now, when i run the script it doesnt display an error message, seems like it runs fine, however it doesnt delete... (10 Replies)
Discussion started by: stride6
10 Replies

2. Shell Programming and Scripting

Something went wrong in the following loop

The script is And the error is + count=2 ./FindEID.ksh: line 227: syntax error near unexpected token `else' ./FindEID.ksh: line 227: ` else' I just can't find out what the problem is, can anyone tell me about this error is? Thank you (5 Replies)
Discussion started by: tpltp
5 Replies

3. Shell Programming and Scripting

What is wrong in my IF loop ??

What is wrong in my IF loop if then echo " The request is authenticated " fi The error im getting is $ ./routing.sh server enables ********************************************************************** Preparing to service the request for Device server in Question... (9 Replies)
Discussion started by: raghunsi
9 Replies

4. Shell Programming and Scripting

Something wrong with while loop

Hi there, i've written a script to extract a portion of a MySQL database table and convert it to CSV and then to import it back as CSV to MySQL. Initially it worked without the while loop but after adding the while loop statement, i am getting the following error: ./export-csv-coordinates.sh:... (4 Replies)
Discussion started by: edge80
4 Replies

5. Shell Programming and Scripting

Whats wrong with my Loop

Hi all, I have been given a task to search for strings in a file that is encoded. I need to display the file name only when all the 3 strings which i provide are present in the file name. i first try to get a list of files according from the directory according to the value passed in argument... (0 Replies)
Discussion started by: amit1_x
0 Replies

6. Shell Programming and Scripting

what's wrong with that loop ?

hello everybody, here's my code mkf () { INDEX=0; while ; do touch "file$1.f"; INDEX=$INDEX+1; done } when I type mkf 10 the loop seems to act infinite and only the last file of the loop is created, in the example below, there just is file10.f in... (11 Replies)
Discussion started by: Oddant
11 Replies

7. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

8. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

9. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

10. Shell Programming and Scripting

What's wrong with this while loop?

function get_tablespace() { ## Get the current size of the tablespace size=`su -l oracle -c 'db-control report' |egrep "DATA_TBS" | awk '{print $5}'|tr -d '%'` ## Loop through until the size is 82 or less count=0 while && do ... (2 Replies)
Discussion started by: bille
2 Replies
break(1)                                                           User Commands                                                          break(1)

NAME
break, continue - shell built-in functions to escape from or advance within a controlling while, for, foreach, or until loop SYNOPSIS
sh break [n] continue [n] csh break continue ksh *break [n] *continue [n] DESCRIPTION
sh The break utility exits from the enclosing for or while loop, if any. If n is specified, break n levels. The continue utility resumes the next iteration of the enclosing for or while loop. If n is specified, resume at the n-th enclosing loop. csh The break utility resumes execution after the end of the nearest enclosing foreach or while loop. The remaining commands on the current line are executed. This allows multilevel breaks to be written as a list of break commands, all on one line. The continue utility continues execution of the next iteration of the nearest enclosing while or foreach loop. ksh The break utility exits from the enclosed for, while, until, or select loop, if any. If n is specified, then break n levels. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be exited. The continue utility resumes the next iteration of the enclosed for, while, until, or select loop. If n is specified then resume at the n- th enclosed loop. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be used. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words that follow a command preceded by ** that are in the format of a variable assignment are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign, and also that word splitting and file name genera- tion are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), exit(1), ksh(1), sh( 1), attributes(5) SunOS 5.10 17 Jul 2002 break(1)
All times are GMT -4. The time now is 07:24 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy