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.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-22-2006
J_ang J_ang is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 15
AWK question?

I have question about awk if someone can help me here. I want to find a string (file has just one row) and then get the position of the column.

let say i have 4 columns

a b c d

Look for b and somehow get the column position in this case b is at $2 script doesn't know what is the position of the column. So if awk or something else can help me to get position of the column.

Thanks in Advance.
  #2 (permalink)  
Old 06-22-2006
Doc_RuNNeR Doc_RuNNeR is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 17
It is necesary to do it with awk?

Because a script can do it

.........
cont=0
for i in `cat (the file)`
do
if [ `echo $i` = (the name you would like to find) ]
then
break;
else
(( cont = cont + 1 ))
done
echo "the position is -$cont-"
.........

Bye

Last edited by Doc_RuNNeR; 06-22-2006 at 09:31 PM..
  #3 (permalink)  
Old 06-22-2006
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Code:
ruby -e 'p gets.split.index("bar").succ' myfile
Code:
newlisp -e '(+ 1(find "foo" (parse (read-line))))' <myfile
  #4 (permalink)  
Old 06-23-2006
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
In awk:
Code:
$ cat file 
a b c d
e c g h
a b j c
$ awk -v var=c '{for (i=0;i<=NF;i++){if(var == $i){ print "Var "var" found in column "i" line "NR }}}' file
Var c found in column 3 line 1
Var c found in column 2 line 2
Var c found in column 4 line 3
  #5 (permalink)  
Old 06-23-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,402
Try...
Code:
echo 'a b c d' | awk 'BEGIN{RS=FS}/b/{print NR}'
  #6 (permalink)  
Old 06-23-2006
J_ang J_ang is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 15
Thanks for everybody's input couldn't accomplish my mission yet

Output of $* (a b c d)

typeset -u P=`echo $*|awk 'BEGIN{RS=FS}/b/{print "$"NR}'`

Rather then giving me b it gives me $2.

Thanks in advance.
  #7 (permalink)  
Old 06-23-2006
J_ang J_ang is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 15
Let me explain little bit what i am trying to accomplish i want to find a string in arugments that have been passed to script then get the column number and based on column number get the value of that arugment.

Thanks
Sponsored Links
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 03:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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