Sub. numbers in column of output with If


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sub. numbers in column of output with If
# 1  
Old 02-11-2007
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  
Old 02-11-2007
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  
Old 02-11-2007
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  
Old 02-11-2007
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  
Old 02-11-2007
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  
Old 02-12-2007
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
# 7  
Old 02-12-2007
"acgstatus" 17 lines, 743 characters
dalsys:dalmso2 > acgstatus

Database selected.
# By MSO Tech II Jason C. Kelsey
db=airgen_cm_db@urban_dbhost
tacg=`echo "select a.neinstanceid, a.almacgcomentseverity, b.neinstanceid, b.nen
ame from nmo_almacgcomentry a, neentry b where a.neinstanceid=b.neinstanceid gro
up by b.neinstanceid, b.nename, a.neinstanceid, a.almacgcomentseverity" | /usr/i
nformix/bin/dbaccess $db`

echo "
| | | |
neinstanceid | almacgcomentsever+ | neinstanceid | nename |
---------------|--------------------|-----------------|----------------|----"


echo $tacg|awk '{ ($2 == 4)? $2 = "Major": $2 = "Critical"} {print}'
~
~
~
6 fewer lines in file after visual
:wq!
-----------------------------------------------------------------------

4452 row(s) retrieved.


Database closed.


| | | |
neinstanceid | almacgcomentsever+ | neinstanceid | nename |
---------------|--------------------|-----------------|----------------|----
awk: syntax error near line 1
awk: illegal statement near line 1
dalsys:dalmso2 > ^C


it is all most there, otherwise it would have spit out a bunch of junk. I see what I can do with it till I hear from you again. ;-) Thx
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Adding Column Of Numbers

Hello. Trying to add a column of numbers and combine the 1st and 2nd fields as uniq with the new total. This works to add the numbers but can't figure an easy was to combine the 1st and 2nd column as the list is very long. awk '{s+=$3} END {print s}' bird dog 300 bird dog 100 cat clown 200... (1 Reply)
Discussion started by: jimmyf
1 Replies

2. Shell Programming and Scripting

Add up a column of numbers

Given a file test.txt ,I can get a list of numbers in a single column using the command : cat test.txt | cut -d ' ' -f 8 that gives the output as 52 52 52 60 52 How can I get the sum of all the numbers in that column that is displayed? i want the output as sum=268 (4 Replies)
Discussion started by: hitha87
4 Replies

3. Shell Programming and Scripting

How to add a column numbers at a particular position?

Problem discription: I have many files which contain the same lines. for instance, (15 lines) file1 ..last column add by hand arbitrarily. 1.78116800 0.68396600 0.00061900 0.47641600 -0.49794500 -0.00024000 -1.70662800 0.29577100 0.67863600 -1.70647600 0.29654600 ... (9 Replies)
Discussion started by: liuzhencc
9 Replies

4. Shell Programming and Scripting

top output for six processes with the same name, output changed from column to row

Hi, I have a system under test, and I use a script that does a ps. The output, is in the following format, it's basically the timestamp, followed by the rss and vsize. 09:03:57 68404 183656 68312 181944 69860 217360 67536 182564 69072 183172 69032 199276 09:04:27 68752 183292 70000 189020... (5 Replies)
Discussion started by: Bloke
5 Replies

5. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

6. UNIX for Dummies Questions & Answers

Increasing numbers in Column

I have UWIn version of Unix for Desktop. I have a file (Subtitle file of a movie) with the following format abc def ghi jkl mno pqr stuv uvw xyz The subtitles are delayed about a min or few seconds more. I want to increase it to be as shown below: abc def ghi jkl mno pqr stuv ... (4 Replies)
Discussion started by: bobbygsk
4 Replies

7. Shell Programming and Scripting

4 column tsv file, output 1 specific column

Hello all siteexplorer.search.yahoo.com can output results in tsv format, when opened in excel I get 4 columns. I would like to wget that file, which I can do. I would then like to pull the 2nd column and output it only. I've searched around and found a few bits and pieces but nothing I've... (6 Replies)
Discussion started by: casphar
6 Replies

8. Shell Programming and Scripting

How to add numbers in a column

Hi All thanks a lot for your previous replies. I need some help here. I am writing a script to test a machine for a thereshold. It is genrating the list of number that have to be added but not displaying the added value. The script is like this #!/bin/sh... (1 Reply)
Discussion started by: asirohi
1 Replies

9. UNIX for Advanced & Expert Users

Adding a column of numbers

Hello, I have a file, and one column has both positive and negative numbers. Does anyone know how I can calculate the total of all the values (i.e, +ve and -ve). eg: col1 col2 col3 data 23 data data 76 data data -30 data Thanks Khoom (1 Reply)
Discussion started by: Khoomfire
1 Replies

10. Shell Programming and Scripting

how to sum numbers in column

Hi, i want to sum all nubers in one column. Example: 12.23 11 23.01 3544.01 I'm trying to do this in awk, but it doesn't work properly. Seems like awk is summing only integers, for example: 12 11 23 3544 It cuts off numbers after dot. I used this command: akw /text/ file.txt |nawk... (1 Reply)
Discussion started by: iahveh
1 Replies
Login or Register to Ask a Question