Help with simple variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with simple variable
# 1  
Old 12-09-2008
Question Help with simple variable

I am working with AIX ksh

I may have an output like one of the following (I am acuatlly using "ps eww <pid>" in a script and would like to assign one of the varable to a varable in my script):
example 1: a=1 b=2 c=3 d=4 e=5 f=6 g=8
or
example 2: e=5 d=3 b=1 a=2 c=4
or
example 3: 5=a 4=b 3=c

I need an easy command that will set a variable with what 3 is equal to:
example 1 I would like VAR=3 (since c was equal to 3)

example 2 I would like VAR=4 (since c was equal to 4)

example 3 I would like VAR= (since there was no c)

Any help would be greatly appreciated.

Last edited by 2dumb; 12-09-2008 at 06:04 PM..
# 2  
Old 12-09-2008
you can assign it to var as shown
Code:
VAR=`echo $c`
or
((VAR=$c))

# 3  
Old 12-09-2008
Quote:
Originally Posted by vidyadhar85
you can assign it to var as shown
Code:
VAR=`echo $c`
or
((VAR=$c))

Sorry I dont think I make my self clear. The following would be the output of a command. They are not yet variables.
example 1: a=1 b=2 c=3 d=4 e=5 f=6 g=8
or
example 2: e=5 d=3 b=1 a=2 c=4
or
example 3: 5=a 4=b 3=c
# 4  
Old 12-09-2008
Hammer & Screwdriver This will give your output

Although, there are many better and quicker ways. If you provided more details on the commands generating the output, there would be better solutions.

Code:
> VAR1=$(echo a=1 b=2 c=3 d=4 e=5 | tr " " "\n" | grep "^c=" | cut -d"=" -f2)
> echo $VAR1
3
>

> VAR1=$(echo 5=a 4=b 3=c 2=d 1=e | tr " " "\n" | grep "^c=" | cut -d"=" -f2)
> echo $VAR1

>

# 5  
Old 12-09-2008
oh ok got it...i think this will do
Code:
VAR=$(echo 5=a 4=b 3=c|awk -F"=" 'BEGIN{RS=" "}/^c/{print $2}')
VAR=$(echo a=1 b=2 c=3 d=4 e=5 |awk -F"=" 'BEGIN{RS=" "}/^c/{print $2}')

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

A simple variable frequency sinewave audio generator.

Hi all... Well I have not been inactive but working out how to make OSX 10.14.x command line audio player have a variable sample rate. This is a back door as afplay does not have a sample rate flag unlike aplay for ALSA, in Linux flavours. This is a DEMO only but a derivative of it will... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Shell Programming and Scripting

Simple Variable substitution in ksh not working

Hi Gurus, Not able to catch what's going wrong here. I just want to get output as "tree". server:/mk/app/nexapp $ echo $SHELL /usr/bin/ksh server:/mk/app/nexapp $ export db_name1="tree" server:/mk/app/nexapp $ export i=1 1st try: server:/mk/app/nexapp $ echo $(db_name$i) ksh: db_name1: ... (3 Replies)
Discussion started by: mukesh.lalwani
3 Replies

3. Shell Programming and Scripting

A simple variable subst is not working

Hi what i want: listing files in a special range ls -lrt 20120601{05..06}* ... -rw-rw-r-- 1 imp imp 279 1. Jun 07:51 201206010550 -rw-rw-r-- 1 imp imp 279 1. Jun 07:01 201206010600 -rw-rw-r-- 1 imp imp 279 1. Jun 07:11 201206010610 -rw-rw-r-- 1 imp imp 279 1. Jun 07:21... (1 Reply)
Discussion started by: IMPe
1 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. Shell Programming and Scripting

"Simple" echo/reading variable question...

Hello, I have a simple(I think) question! Although simple, I have been unable to resolve it, so I hope someone can help! OK, here it is: 1)I have an awk script that prints something, such as: awk '{print $2}' a > x so x might hold the value of say '10' 2)Now, I just want to check to see if... (4 Replies)
Discussion started by: astropi
4 Replies

6. Shell Programming and Scripting

Simple sed variable substitution

Give the code: set line = 2 set year = `sed -n '2p' file while ($line < 500) echo $line > f.txt @ line = $line + 1 end How do I utilize the variable $line in the code instead of the number 2. I'm using this in a while loop and counter. I've tried quoting it, double/single... (1 Reply)
Discussion started by: wxornot
1 Replies

7. Shell Programming and Scripting

simple assigning variable question

I know many will probably say go read some tutorials...I would like to say that I am. However, I need to know this for work and it needs to be done soon. In my script I have a while loop that reads a .tbl file of 4 columns and assigns them to variables a, b, c, d. Once in the loop i do ... (10 Replies)
Discussion started by: questionasker
10 Replies

8. Shell Programming and Scripting

Simple script to return environment variable

HI , In the below script I am trying to return the value of the environment variable TIBCO_HOME to the caller #! /usr/bin/csh set VAR_NAME=$1 echo VAR_NAME On the aix console.. set to setenv TIBCO_HOME /app/tibco When I execute the script... myscript.sh TIBCO_HOME, the script... (5 Replies)
Discussion started by: bce_groups
5 Replies

9. UNIX for Dummies Questions & Answers

Very simple question about changing PS1 variable at startup!

Hello there ! I am new in this Unix world and just start learning Unix. I have very simple question about changing PS1 variable (Shell Prompt) i have local.profile file in my working directory, i open in vi edit mode and add this line PS1="Hello:>" and i save that file. I disconnected from... (2 Replies)
Discussion started by: abidmalik
2 Replies

10. Shell Programming and Scripting

simple SH variable

Greetz all, I'm using OBSD 2.9 with /bin/sh as my shell. I want to modify the SH prompt to reflect my current working directory. I'm new to shell programming, so pardon if I'm going about things wrong ;) I thought I'd be able to do it with the following line: PS1="`pwd`# " but of course,... (9 Replies)
Discussion started by: sancho5
9 Replies
Login or Register to Ask a Question