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
Insert a line including Variable & Carriage Return / sed command as Variable lowmaster Shell Programming and Scripting 2 05-20-2009 12:26 AM
Sed variable substitution when variable constructed of a directory path alrinno Shell Programming and Scripting 2 07-11-2008 03:24 PM
Enviornment Variable in B shell (I call it nested variable) princelinux Shell Programming and Scripting 4 07-02-2008 02:35 AM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 03:56 PM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 12:29 PM

Reply
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 07-07-2009
dplate07
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Using AWK as variable?

hello guys,
hope u can help im trying to integrate awk in a variable and then display the values in the variable which i would then use as a menu.

my code goes like this ....

OPTIONS= awk '{print $1}' /etc/fstab

select opt in $OPTIONS; do
if [ "$opt" = "$opt" ]; then #the code is $opt = $opt because $OPTIONS contains alot of other values. while opt already has a selected value#
echo "executing..."
done

....
....
  #2 (permalink)  
Old 07-07-2009
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2005
Location: Switzerland - GE
Posts: 1,575
something like:
Code:
export OPTIONS=$(awk '{print $1}' /etc/fstab)
?
  #3 (permalink)  
Old 07-07-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,055
You're not far away with your OPTIONS= bit.

Are you sure you want $1 from fstab, and not $2 (the FS mount point)?

In any case fstab may contain comments and blank lines, so you should remove these.

Code:
set -A OPTIONS $(grep -Ev "^#|^$" /etc/fstab | awk '{print $1}')
to put the values into an array. You can access the values as
Code:
echo ${OPTIONS[0]}
echo ${OPTIONS[1]}
etc.
or to put them into a single variable

Code:
OPTIONS=$(grep -Ev "^#|^$" /etc/fstab | awk '{print $1}')
Update: Oh yes, and the "export OPTIONS" doesn't hurt.
  #4 (permalink)  
Old 07-07-2009
dplate07
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
An update to this problem :

im thinking of using

awk '$2 ~ /^\/home/ {sub(/$/,",usrquota",$4); print; next} {print}' infile

to search for specific column and replace it with other values. However, i want to use the $opt (assume that i use mountpoint in this variable) , thus it would look something like this

awk '$2 ~ /^/$opt/ {sub(/$/,",usrquota",$4); print; next} {print}' infile

and thus want this line to edit column based on what the user selects. However, im getting errors.
  #5 (permalink)  
Old 07-07-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,055
By "specific columns" I read you to mean "specific values in specific columns"?

The code
Code:
awk '$2 ~ /^/$opt/ {sub(/$/,",usrquota",$4); print; next} {print}' infile
won't work because $opt (I presume) is a shell variable, not an awk one (which don't have a leading $ (except built-in ones)).

Code:
awk '$2 ~ opt {sub(/$/,",usrquota",$4); print; next} {print}' opt="^$opt" infile
Reply

Bookmarks

Tags
awk, variables

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 05:06 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