How to: Parse text string into variables using Korn shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to: Parse text string into variables using Korn shell
# 1  
Old 05-22-2008
How to: Parse text string into variables using Korn shell

I am writing a script to keep check on free disk space, and I would like to find a way to parse $LINE (see code below) into a numeric value (for free disk space percentage) and a string value (for mount point). If possible, I would like to avoid sed or any additional use of awk since I am not very familiar with those utilities. All help is appreciated. Thanks in advance.

Code:
ScriptName=$(basename $0)			# Equivalent shorthand code is: ${0##*/}

TempFile_NME=${DBA_TMP}/${ScriptName}.tmp

df -h | grep / | awk '{ print $5, $6 }'>$TempFile_NME

while read LINE
	do
		echo "$LINE"
	done < $TempFile_NME

# 2  
Old 05-22-2008
Question Please show some intermediate output

Your script writes out line to the screen. Can you paste a few lines, or all lines, into a follow-up message? Especially since it appears that there is more than one field of information being displayed.
Most likely a cut command can pull the data apart.
# 3  
Old 05-22-2008
Quote:
Originally Posted by joeyg
Your script writes out line to the screen. Can you paste a few lines, or all lines, into a follow-up message? Especially since it appears that there is more than one field of information being displayed.
Most likely a cut command can pull the data apart.
The data looks like this:

Code:
15% /
0% /devices
0% /system/contract
0% /proc
0% /etc/mnttab
1% /etc/svc/volatile
0% /system/object
0% /dev/fd
1% /var/run
50% /s01
40% /s02
20% /u01

Note that the "/" varies in its position. I need to find a way to take that into account.
# 4  
Old 05-22-2008
Power Here is one approach, and it combines a couple different tricks

Code:
> cat inputf
15% /
0% /devices
0% /system/contract
0% /proc
0% /etc/mnttab
1% /etc/svc/volatile
0% /system/object
0% /dev/fd
1% /var/run
50% /s01
40% /s02
20% /u01

Code:
> cat scan_data 
#! /bin/bash
while read zf
   do
   numb=$(echo "$zf" | cut -d"%" -f1)
   driv=$(echo "$zf" | cut -d"%" -f2)

   drivt=$(printf "%-40s" "$driv")
   numbt=$(printf "%9.3f" "$numb")
   echo "For your disk drive = $drivt ... your percentage full is $numbt"

done<inputf

Code:
> scan_data 
For your disk drive =  /                                       ... your percentage full is    15.000
For your disk drive =  /devices                                ... your percentage full is     0.000
For your disk drive =  /system/contract                        ... your percentage full is     0.000
For your disk drive =  /proc                                   ... your percentage full is     0.000
For your disk drive =  /etc/mnttab                             ... your percentage full is     0.000
For your disk drive =  /etc/svc/volatile                       ... your percentage full is     1.000
For your disk drive =  /system/object                          ... your percentage full is     0.000
For your disk drive =  /dev/fd                                 ... your percentage full is     0.000
For your disk drive =  /var/run                                ... your percentage full is     1.000
For your disk drive =  /s01                                    ... your percentage full is    50.000
For your disk drive =  /s02                                    ... your percentage full is    40.000
For your disk drive =  /u01                                    ... your percentage full is    20.000
For your disk drive =                                          ... your percentage full is     0.000

# 5  
Old 05-23-2008
Joeyg,

This is wonderful! My background is in VMS scripting, and I've been looking for a Korn equivalent to DCL F$FAO for a long time. This is the closest I've seen.

Thanks for the snippet of code. I have what I need now.

Have a great weekend!

J
# 6  
Old 05-23-2008
Code:
numb=$(echo "$zf" | cut -d"%" -f1)

Apparently, numb is a string. Can you tell me how convert it to integer and subtract it from 100? That would let me display the output as % free, rather than % full.

Thanks again!

j
# 7  
Old 05-23-2008
Code:
numb=$(expr $zf - 100)

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse variables from C++ to shell

Hi All, #include <iostream> int main() { std::int foo = 34; system("mkdir /home/linuxUser/fooDir"); system("vi fooFile") system("write foo in fooFile") foo = 43; foo = read foo from fooFile; std::cout << "foo = " << foo ; } result should be foo = 34 can... (3 Replies)
Discussion started by: linuxUser_
3 Replies

2. Shell Programming and Scripting

'*' vs. '@' in Korn Shell Array Variables

In order to use the shellcurses functions described at: Shell Curses function library I am learning about ksh, which has arrays. My trusty Kochan & Wood book says that for any Korn Shell array AR : ${AR } expands to all the defined array elements, and ${#AR } expands to the number... (3 Replies)
Discussion started by: Clovis_Sangrail
3 Replies

3. Shell Programming and Scripting

korn shell: check the content of a string of a variable

hello, i have a variable which should have following content : var="value1" or var="value2" or var="value2:*" # example: value2:22 how can i check : - if the content is ok (value1 / value2* ) - the two options of "value2" when content is example "value2:22" , i want to split... (3 Replies)
Discussion started by: bora99
3 Replies

4. Shell Programming and Scripting

Korn shell program to parse CSV text file and insert values into Oracle database

Enclosed is comma separated text file. I need to write a korn shell program that will parse the text file and insert the values into Oracle database. I need to write the korn shell program on Red Hat Enterprise Linux server. Oracle database is 10g. (15 Replies)
Discussion started by: shellguy
15 Replies

5. Shell Programming and Scripting

floating point variables korn shell

Hi I'm not using Korn93 but want to use floating point variable. Is there any solution to do that ? thx for help. ---------- Post updated at 02:28 PM ---------- Previous update was at 12:38 PM ---------- I have the following peace of code: for n in `cat log.January.1.array` do ... (3 Replies)
Discussion started by: presul
3 Replies

6. Shell Programming and Scripting

String parsing in Korn Shell

Hi everybody, I have a string stored in a variable called record: record="SNMPv2-SMI::ent.9.9.43.1.3.9.2 = Timeticks: (177330898) 20 days, 12:35:08.98" I want to write some regular expressions good for Korn Shell to extract the number between parenthesis, in this case 177330898, and put it in... (3 Replies)
Discussion started by: omoyne
3 Replies

7. Shell Programming and Scripting

How to parse a string into variables

I'm working in korn shell and have a variable which contains a string like: aa_yyyymmdd_bbb_ccc_ddd.abc. I want to treat the _ and . as delimiters and parse the string so I end up with 6 values in variables that I can manipulate. My original plan was to use var1=`echo $sting1 | cut -c1-c2` but... (9 Replies)
Discussion started by: aquimby
9 Replies

8. UNIX for Advanced & Expert Users

Accessing PL/SQL OUT variables in Korn Shell Script

Hello All, I was just wondering if there is any direct way to access PL/SQL OUT variables from Korn Shell Script. I could already figure out how to return a single value back from PL/SQL to Shell Script (using bind variable). But, what if we want to return multiple values? One option I... (4 Replies)
Discussion started by: bright_future
4 Replies

9. Shell Programming and Scripting

How to parse config variables from external file to shell script

How do i use a config.txt to recursively pass a set of variables to a shell script eg my config.txt looks like this : path=c://dataset/set1 v1= a.bin v2= b.bin path=c://dataset/set2 v1= xy.bin v2= abc.bin .................. and so on . and my testscript : (2 Replies)
Discussion started by: pradsh
2 Replies

10. Shell Programming and Scripting

Variables Naming in Korn Shell

Hi every body, I want to know what is the different between the following: $VAR1 ${VAR2} "${VAR3}" Are they equivalent? Thanks in advance (1 Reply)
Discussion started by: aldowsary
1 Replies
Login or Register to Ask a Question