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
print line before column 1 transitions ajp7701 Shell Programming and Scripting 2 04-17-2008 11:05 PM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
Print row if value in column 1 is the first occurence Raynon Shell Programming and Scripting 7 03-18-2008 02:31 AM
to print column using awk cdfd123 Shell Programming and Scripting 2 07-26-2007 01:15 PM
Print last 4 columns (variable column #) Da_Duck UNIX for Dummies Questions & Answers 19 02-27-2004 10:33 AM

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 09-15-2006
jambesh's Avatar
jambesh jambesh is offline
Registered User
  
 

Join Date: Aug 2006
Location: Pune,India
Posts: 137
can awk print column using a variable ??

i want to print the column file using awk or cut in dynamic manner
like

trmp=2;temp1=1;temp3=2

awk 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt

or cut -f $temp1,$temp2,$temp3 -d"\t" file_name .

but it is showing error , In awk can i use variable as in printing the column ??

I use cut also in the following way in my shell script

cut -f $temp3,$temp1,$temp -d"\t" client_data.txt


assist me
  #2 (permalink)  
Old 09-15-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
awk -v temp=2 -v temp1=1 -v temp3=3 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt
  #3 (permalink)  
Old 09-15-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by anbu23
Code:
awk -v temp=2 -v temp1=1 -v temp3=3 'BEGIN{OFS=FS="\t"} {print $temp,$temp1,$temp3}' client_data.txt
anbu23,
awk's InputFieldSeparator variable is 'FS'.
shell's InputFieldSeparator variable is 'IFS'.
  #4 (permalink)  
Old 09-15-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Quote:
Originally Posted by anbu23
Code:
awk -v temp=2 -v temp1=1 -v temp3=3 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt
Woah, I didn't know FOO=BAR="value" was legal in awk. Good to know!
  #5 (permalink)  
Old 09-15-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
temp=2 ;temp1=1 ;temp3=3
awk 'BEGIN{OFS=FS="\t"} {print "'"$temp"'" " " "'"$temp1"'" " " "'"$temp3"'" }' client_data.txt
  #6 (permalink)  
Old 09-18-2006
jambesh's Avatar
jambesh jambesh is offline
Registered User
  
 

Join Date: Aug 2006
Location: Pune,India
Posts: 137
vgersh,

I tried with this code but it is saying --

f1=${ccseq[0]};f2=${ccseq[1]};f3=${ccseq[2]};f4=${ccseq[3]};f5=${ccseq[4]};f6=${ccseq[5]}

awk -v c1=$f1 -v c2=$f2 -v c3=$f3 -v c4=$f4 -v c5=$f5 -v c6=$f6 'BEGIN{OFS=FS="\t"} {print $c1,$c2,$c3,$c4,$c5,$c6}' ${fname}

awk: syntax error near line 1
awk: bailing out near line 1

can you please help me out !
I want the column value could be anything between 1-6 so i have tried to assign c=$f1 ,c2=$f2,....here f1 ,f2 ,....will be generating the proper column sequence between 1 to 6 and passed to awk to print ..but i am failed to do so.

I am using SunOS.

Last edited by jambesh; 09-18-2006 at 01:29 AM..
  #7 (permalink)  
Old 09-18-2006
jambesh's Avatar
jambesh jambesh is offline
Registered User
  
 

Join Date: Aug 2006
Location: Pune,India
Posts: 137
the fixed look up column sequence string is
"MANDT SERAIL SERSCHA SEREX EQTYP BSTVP "


Shell Script i had written that actually determine the correct sequence for an input file.
Script:

####Use lookup_data.sh client_file###########

fname=$1
set -A lookup_string MANDT SERAIL SERSCHA SEREX EQTYP BSTVP
set -A cseq
set -A Headr `head -1 ${fname}`
echo ${Headr[*]}
i=0
while [ $i -lt ${#lookup_string[@]} ]
do
j=0
while [ $j -lt ${#Headr[@]} ]
do
if [ "${lookup_string[$i]}" = "${Headr[$j]}" ]
then
echo "look up and file column equal at $j "
cseq[$i]=$j
break
fi
((j=j+1))
done
((i=i+1))
done
i=0
while [ $i -lt ${#cseq[@]} ]
do
ccseq[$i]=`expr ${cseq[$i]} + 1`
echo " ${ccseq[$i]} : \c "
((i=i+1))
done
echo
f1=${ccseq[0]};f2=${ccseq[1]};f3=${ccseq[2]};f4=${ccseq[3]};f5=${ccseq[4]};f6=${ccseq[5]}
awk -v c1=$f1 -v c2=$f2 -v c3=$f3 -v c4=$f4 -v c5=$f5 -v c6=$f6 'BEGIN{OFS=FS="\t"} {print $c1,$c2,$c3,$c4,$c5,$c6}' ${fname}




Input file :
cat client_data.txt
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

running the script as $ script_name.sh client_data.txt
giving correct sequence but unable to print in that sequence ...

please assist

can any body tell me why red colored two line are not working ??????

Last edited by jambesh; 09-18-2006 at 03:10 AM..
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 12:07 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