The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
can someone help me with modifying this file eamani_sun Shell Programming and Scripting 2 05-22-2008 10:15 AM
Need help in modifying the prompt mahatma Shell Programming and Scripting 7 08-02-2006 12:45 AM
modifying my shell nico-hellas Shell Programming and Scripting 1 08-05-2005 06:43 AM
modifying the writing of a log... Nicol Shell Programming and Scripting 2 04-13-2005 07:05 AM
Modifying $PATH variable in /etc/profile Deepali UNIX for Dummies Questions & Answers 3 03-12-2001 08:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 16
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Modifying a variable value

Hi all, how do i modify a variable's value.

var1='abcd efgh ijkl mnop abcd'

how do i get var2 from var1

var2=$(......)
$echo var2
abcd efgh ijkl mnop

i.e. i have removed a duplicate occurence.

or in general how to modify a varible.

thanks in advance
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
It's not at all clear what concept you are looking for. Perhaps you could elaborate on that a bit. In the meantime, here are some hopefully useful exercises.

Code:
var2=$var1   #copy var1 to var2
var2=${var2%abcd}  # trim abcd from end, if present
var2=`echo "$var2" | tr ' ' '\012' | sort | uniq | tr '\012' ' '` # remove duplicate tokens
The token duplicate removal is probably quite different from what you are imagining it would be, and has the unfortunate side effect of sorting the remaining tokens. In this case it doesn't matter, because they were already sorted, but nevertheless it's not a good general-purpose solution if the order of tokens is significant.

Perhaps you should read a tutorial on shell programming at this point; there's a lot of things you can do with variable substitution (${var%trim} is but a single example).
Reply With Quote
  #3 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 16
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
hi era

I am used to programming in Matlab and C++ and in these languages you can refer to individual members of a variable (array) and compare their values to all the other members using 2 loops. how can i accomplish that in unix. For example

var2=$(users)

will return the current users but if a user is running two instances of a shell then that username will be returned twice. How can i trim the varible $var2 so that multiple occurrences of usernames are eliminated. i can use a for loop to loop through each member of the varible but then how do i compare it to the rest of the usernames in $var2.

I am referring to tutorials as well but most of them jus explain the general syntax of commands. i guess i am not referring to the right ones, will work on tht.
Reply With Quote
  #4 (permalink)  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
In fact my sort | uniq example was not so far off the mark then.

A variable in Bourne classic is just a piece of text; some shells such as bash and ksh have array variables, too. The common trick would be to modify the text so it is in a suitable form by the time you assign it to a variable.

Code:
var2=$(users | sort | uniq)
Reply With Quote
  #5 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 356
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Code:
var2=$(users | awk '{for(i=1; i<=NF; ++i) if(!a[$i]++) printf("%s ", $i)}')
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB 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 -7. The time now is 05:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102