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:

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:

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:

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:

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!!!

I can't help with csh, but one question jumps to my mind: if using awk anyhow, why not do the entire task with it?

I also tried something like that

awk '{
if ($1 >=2014)
        print $0;
else
        print $1, $4, $2, $3;
}'

Could not make it work too...

Echoing Rudic's comments, you can try below:

awk 'NF==4{sub($1,"&"$NF);sub(/. *$/,"")}1' pha.dat

Update: Realised you wanted to remove the 4th column as well.

Try this small adaption to your awk attempt:

awk '{
if ($1+0 >= 2014)
        print $0;
else
        print $1$4, $2, $3;
}' file
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

or, try

awk '$1 !=  "2014"    {print $1$4, $2, $3; next}
     1
    ' file

I use the command

awk -f sc.awk pha.dat > dnm.out

Did not work!
What is the correct command to run awk script?

Why did it not work, what error did you receive if any? What exactly is the content of sc.awk?

The -f flag is the correct flag to invoke an awk script from the command line. Also just to point out with regards to the method being used in the awk script, it will not retain the original spacing.

the sc.awk is

awk '$1 !=  "2014"    {print $1$4, $2, $3; next}
     1
    ' pha.dat

and run it with

awk -f edit2.awk pha.dat > dnm.out

I will make other adjustments, I'll handle the spacing.

The suggestion which I gave earlier will retain the spacing FYI. The awk script file need only look like this:

$1 !=  "2014"    {print $1$4, $2, $3; next}
     1

Yes! I got it now!

Thank you.

Problem solved.