What's the correct syntax to use "if-else"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's the correct syntax to use "if-else"
# 1  
Old 11-06-2009
Power What's the correct syntax to use "if-else"

hi, i'm a beginner in Linux, actually i use andLinux,
i have a data file with 11 columns
Code:
170 N  SER T 25 56.962 42.701 -133.423 1.00 87.04 N
171 CA SER T 25 57.076 41.790 -132.280 1.00 86.65 C
172 C  SER T 25 57.766 40.492 -132.692 1.00 87.52 C
173 O  SER T 25 58.653 39.988 -131.992 1.00 86.59 O
174 CB SER T 25 55.694 41.451 -131.730 1.00 85.78 C
175 OG SER T 25 54.981 40.607 -132.622 1.00 77.49 O

i would like write close to 11 th column "12" if in the 11 th column is C or "14" if is N or "16" if is O,
i tried several time only for C but doesn't work
Code:
{ 
atom = $11
if  ( atom == " C " ) 
{ 
  print  $11 12
} 
else
{ 
  print  $11 "no" 
}
}

Can somebody help me???
thanks

Last edited by Franklin52; 11-06-2009 at 05:52 PM.. Reason: Adding code tags and aligning code
# 2  
Old 11-06-2009
"==" is Perl syntax.

Try "man test"
# 3  
Old 11-06-2009
bash script
Code:
while read LINE
do
    Atom=$(echo $LINE | cut -d' ' -f11)
    case $Atom in
        C )    echo "$Atom 12" ;;
        N )    echo "$Atom 14" ;;
        O )    echo "$Atom 16" ;;
        * )    echo "$Atom no" ;;
    esac
done < data

result with your data sample :
Code:
N 14
C 12
C 12
O 16
C 12

# 4  
Old 11-07-2009
Code:
 awk '{
if ($NF=="N") {print $11,"14"}
else if ($NF=="C") {print $11,"12"}
     else if ($NF=="O") {print $11,"16"}
           else {print $11,"no"}
} ' urfile
N 14
C 12
C 12
O 16
C 12
O 16


Last edited by rdcwayx; 11-08-2009 at 11:28 PM..
# 5  
Old 11-08-2009
Code:
sed -e 's/N$/N12/' -e 's/C$/C14/' -e 's/O$/O16/'

# 6  
Old 11-08-2009
Quote:
Originally Posted by summer_cherry
Code:
sed -e 's/N$/N12/' -e 's/C$/C14/' -e 's/O$/O16/'

There is one more request, if not N$, C$, O$, should print "no".
# 7  
Old 11-10-2009
reply

Thanks so much, but it still no working, if i prepare a file test.awk and i call it by command awk -f test.awk , it writes a column of "no". And if i try to write the command directly on the terminal, it writes nothing and the terminal become useless. Smilie

---------- Post updated at 12:59 PM ---------- Previous update was at 10:56 AM ----------

thanks a lot, with your suggestion it works.
thank you.
ElsaSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. OS X (Apple)

"Permission denied" when trying to SSH my iPhone though password is correct

Hi, I hope this is the correct section in the forum to post as I'm trying to SSH from my MacBook. I was looking to see whether ssh on my jailbroken iPhone 6s (10.3.1) still works fine and was following this old reddit guide. I installed OpenSSH&OpenSSL from Cydia and changed the password using... (7 Replies)
Discussion started by: hss1
7 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

Compare file names and select correct elements to include in "for each loop"

Hi everyone, I`ll try to be most clear I can explaining my help request. I have 2 folders Folder A-->This folder receives files through FTP constantly Folder B-->The files from Folder A are unzipped and then processed in Folder B Sometimes Folder A doesn`t contain all... (2 Replies)
Discussion started by: cgkmal
2 Replies

5. Shell Programming and Scripting

Need Correct syntax for "if" in UNIX

I am trying to write a simple if statement but that driving me crazy:eek: with syntactical erorrs. This is what I managed to come up #!/bin/ksh USERNAME=`whoami` if ;then echo " you have logged in as report user" elif ; then echo " you have logged in as extract user" else " I dont... (3 Replies)
Discussion started by: kkb
3 Replies

6. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

7. Shell Programming and Scripting

Pls correct the "if" syntax

Iam a learner of UNIX. Fortunately I got this site. I want to check the file for its existance. if echo " Not present" else echo "Present" fi The above code is working fine. But I also want to check for the files which are compressed. I tried the following code and it is not... (5 Replies)
Discussion started by: ganapati
5 Replies

8. UNIX for Dummies Questions & Answers

correct syntax for using "grep regex filename" ?

I'm trying to grep a long ls by looking at the beginning of each filename for example: Many files begin with yong_ho_free_2005... Many files begin with yong_ho_2005... I can't just use "grep yong_ho" otherwise It'll display both files. So I'm trying to use a regex but my syntax is wrong. ... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question