The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
if [ $NOWDATE -gt $STARTDATE ] , date comparison correct syntax? danpaluska Shell Programming and Scripting 1 2 Weeks Ago 06:24 PM
Plz correct my syntax of shell script girish.batra Shell Programming and Scripting 2 06-06-2008 06:36 AM
Pls correct the "if" syntax ganapati Shell Programming and Scripting 5 07-31-2006 02:07 AM
correct syntax for using "grep regex filename" ? yongho UNIX for Dummies Questions & Answers 2 06-13-2005 02:44 PM
Correct Syntax For Calling Shell Script in Perl Module mh53j_fe Shell Programming and Scripting 4 06-08-2005 10:42 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
elsa elsa is offline
Registered User
  
 

Join Date: Nov 2009
Location: Bellinzona
Posts: 2
Red face 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; 2 Weeks Ago at 05:52 PM.. Reason: Adding code tags and aligning code
  #2 (permalink)  
Old 2 Weeks Ago
TonyLawrence TonyLawrence is offline
Registered User
  
 

Join Date: Sep 2007
Location: SE Mass
Posts: 137
"==" is Perl syntax.

Try "man test"
  #3 (permalink)  
Old 2 Weeks Ago
frans's Avatar
frans frans is offline
Registered User
  
 

Join Date: Oct 2009
Location: Drôme, France
Posts: 86
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 (permalink)  
Old 2 Weeks Ago
rdcwayx rdcwayx is online now
Registered User
  
 

Join Date: Jun 2006
Posts: 207
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; 2 Weeks Ago at 11:28 PM..
  #5 (permalink)  
Old 2 Weeks Ago
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
Code:
sed -e 's/N$/N12/' -e 's/C$/C14/' -e 's/O$/O16/'
Bits Awarded / Charged to summer_cherry for this Post
Date User Comment Amount
2 Weeks Ago Anonymous Always see your reply with perl script, and now i know, you are shell script expert also. 100
  #6 (permalink)  
Old 2 Weeks Ago
rdcwayx rdcwayx is online now
Registered User
  
 

Join Date: Jun 2006
Posts: 207
Quote:
Originally Posted by summer_cherry View Post
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 (permalink)  
Old 1 Week Ago
elsa elsa is offline
Registered User
  
 

Join Date: Nov 2009
Location: Bellinzona
Posts: 2
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.

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

thanks a lot, with your suggestion it works.
thank you.
Elsa
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0