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
Simple to you not simple to me pattern matchin help aleks001 Shell Programming and Scripting 0 07-22-2007 11:06 PM
Simple C question... Hopefully it's simple Xeed High Level Programming 6 12-15-2006 02:29 PM
Ok simple question for simple knowledge... Corrail UNIX for Dummies Questions & Answers 1 11-28-2005 01:03 PM
Very simple question about changing PS1 variable at startup! abidmalik UNIX for Dummies Questions & Answers 2 08-26-2002 02:05 AM
simple SH variable sancho5 Shell Programming and Scripting 9 01-04-2002 03:16 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-09-2008
2dumb 2dumb is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 40
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 (permalink)  
Old 12-09-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
you can assign it to var as shown
Code:
VAR=`echo $c`
or
((VAR=$c))
  #3 (permalink)  
Old 12-09-2008
2dumb 2dumb is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 40
Quote:
Originally Posted by vidyadhar85 View Post
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 (permalink)  
Old 12-09-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink 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 (permalink)  
Old 12-09-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
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}')
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:54 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