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
Shell script to search through numbers and print the output cdfd123 Shell Programming and Scripting 8 10-30-2007 04:40 PM
output only numbers from mixed string nortypig Shell Programming and Scripting 10 08-29-2006 10:30 PM
Adding a column of numbers Khoomfire UNIX for Advanced & Expert Users 1 01-18-2006 04:55 AM
formatting output in human readable numbers ghazi Shell Programming and Scripting 11 01-10-2005 01:31 PM
how to sum numbers in column iahveh Shell Programming and Scripting 1 09-30-2004 07:50 AM

Closed Thread
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 Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
  #1 (permalink)  
Old 02-11-2007
ja156194's Avatar
ja156194 ja156194 is offline
Registered User
  
 

Join Date: Feb 2007
Location: Plano
Posts: 9
Sub. numbers in column of output with If

This is my script. I am pulling the status of some hard where, but the status is in numerical form. The number 4 means Major and the 5 means Critical. In my script I would like to show the alarm type in aplha rather than numeric form. So if instead of seeing a 4 or 5 you would see MAjor or Critical. I want to use an if becuase I have a few more types but I am trying to keep it simple so I can grasp the concept. I also attached a sample out of my commmand file.

dalsys:dalmso2 > more acgstatus
#!/bin/ksh
# Description:
# This script will pull load info for network NEs.
# Nextel Dallas
# By MSO Tech II Jason C. Kelsey
db=airgen_cm_db@urban_dbhost

tacg=`echo "select a.neinstanceid, a.almacgcomentseverity, b.neinstanceid, b.nename from nmo_almacgcomentry a, neentry b where a.neinstanceid=b.neinstance
id group by b.neinstanceid, b.nename, a.neinstanceid, a.almacgcomentseverity" | /usr/informix/bin/dbaccess $db`

echo "
| | | |
b.neinstanceid.| b.nename | a.neinstanceid.a|a.almacgcomentseverity|
---------------|-----------|-----------------|----------------------|----"

echo $tacg

if [ "" = "4" ]
then {
="Major"
}
fi

if [ "" = "5" ]
then {
="Critical"
}
fi

Output currently:
Here is the put without the if statments...........................
dalsys:dalmso2 > acgstatus
Database selected.
4452 row(s) retrieved.
Database closed.
| | | |
neinstanceid | almacgcomentsever+ | neinstanceid | nename |
---------------|--------------------|-----------------|----------------|----
neinstanceid almacgcomentsever+ neinstanceid nename

9041 4 9041 NTX0322R_IDBEACHBRD
32555 5 32555 NOK3410R_IZNEMIAMI
64586 4 64586 PTX389PR_IVSEBRNVIL
1176 4 1176 NTX1401R_IHCENTERFI
470 4 470 NTX0182R_IDOOOI35_U
62737 4 62737 PTX324PR_ICN_ALICE
63715 4 63715 PTX569PR_IWATLANTA
65711 5 65711 PTX123PR_IBSLUMBERT
69746 4 69746 NTX1822R_IHKATYHAWK
28013 4 28013 NTX1407R_IHFONVILLA
448 4 448 NTX0130R_IDGARLAND

I would be much appricated, I have been at since 2am.. ;-)

Last edited by ja156194; 02-11-2007 at 11:26 AM.. Reason: typo
  #2 (permalink)  
Old 02-11-2007
hegemaro hegemaro is offline
Registered User
  
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 134
The easiest way that I can think of is to use sed(1) rather than a shell if statement. It's quite simple. Your output is saved in file but can also be piped into the sed command.

# cat file | sed -e "s/^\([0-9][0-9]*\) 4 /\1 Major /" -e "s/^\([0-9][0-9]*\) 5 /\1 Critical /"
9041 Major 9041 NTX0322R_IDBEACHBRD
32555 Critical 32555 NOK3410R_IZNEMIAMI
64586 Major 64586 PTX389PR_IVSEBRNVIL
1176 Major 1176 NTX1401R_IHCENTERFI
470 Major 470 NTX0182R_IDOOOI35_U
62737 Major 62737 PTX324PR_ICN_ALICE
63715 Major 63715 PTX569PR_IWATLANTA
65711 Critical 65711 PTX123PR_IBSLUMBERT
69746 Major 69746 NTX1822R_IHKATYHAWK
28013 Major 28013 NTX1407R_IHFONVILLA
448 Major 448 NTX0130R_IDGARLAND


To add additional value translations, just and another "-e script" option. The syntax is

-e 's/^\([0-9][0-9]*\) number /\1 string /"

which says, on a line starting with one or more digits, then a space, another number, and one more space, substitute with the original starting digits, a space, a new string and a space.
  #3 (permalink)  
Old 02-11-2007
ja156194's Avatar
ja156194 ja156194 is offline
Registered User
  
 

Join Date: Feb 2007
Location: Plano
Posts: 9
thanks but sed is an easy fix..

I am trying to get a bit more into the arguementive script, because I have another script I am working on and sed would become a problem. I would like to keep it simple and neat. I apprecate the reply though and any more details will be very help full. Thanks again. I am just looking to get the understanding of a simple if statement. I am one that will not stop until I complete something that has been stumping me. This if statement has, I have been battling now going on 20hrs straight.

Thanks Get ur Done.|
  #4 (permalink)  
Old 02-11-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
You can use awk for this job, maybe this is easier to understand:

Code:
awk '{ ($2 == 4)? $2 = "Major": $2 = "Critical"} {print}' datafile
If $2 = 4 then $2 = "Major" else $2 = "Critical"; print the line

Regards
  #5 (permalink)  
Old 02-11-2007
ja156194's Avatar
ja156194 ja156194 is offline
Registered User
  
 

Join Date: Feb 2007
Location: Plano
Posts: 9
awk ?

Now i am pretty familar with awk but it appears you are using it in a csh format. Am I mistaken? I ihave payed with it abit and continuiously get a awk statement failure.

It appears it woud work can you help me a bit more by giving me example using my script above. I have been negecting my house work. I am a singe father, so you know the wash is stacked, and the dished are growing, but I am determinded to competed this script before I wak into work tomorrow.

My co workers and boss are all ignorate to scripting and see no reason of logic behind using it. they think I am crazy, I have worked in telecom for 6 years, and never did I want, have, or even used a gui point and click.

There is no CLI with the network I am on. Moto got them locked to a GUI patform. So I have taken it apon my mysef to dig out of informix / dbaccess and use the mib to get accual and factual data. Long story short, it wil.l feel goood to rub this script it there face, that they have snickered amoung them seleves saying it could not be done.

Thanks 4 everyones support .|
  #6 (permalink)  
Old 02-12-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Quote:
Now i am pretty familar with awk but it appears you are using it in a csh format
No, I'm using Bash and it also works with the ksh on a HP-UX system.

Try this in your script:
Code:
echo $tacg|awk '{ ($2 == 4)? $2 = "Major": $2 = "Critical"} {print}'
instead of this:
Code:
echo $tacg
f [ "" = "4" ]
then {
="Major"
}
fi

if [ "" = "5" ]
then {
="Critical"
}
fi
Regards
Closed Thread

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 07:01 PM.


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