The UNIX and Linux Forums  

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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-15-2006
jambesh's Avatar
Registered User
 

Join Date: Aug 2006
Location: Pune,India
Posts: 118
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
Reply With Quote
Forum Sponsor
  #2  
Old 09-15-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
awk -v temp=2 -v temp1=1 -v temp3=3 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt
Reply With Quote
  #3  
Old 09-15-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
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'.
Reply With Quote
  #4  
Old 09-15-2006
Glenn Arndt's Avatar
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!
Reply With Quote
  #5  
Old 09-15-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
temp=2 ;temp1=1 ;temp3=3
awk 'BEGIN{OFS=FS="\t"} {print "'"$temp"'" " " "'"$temp1"'" " " "'"$temp3"'" }' client_data.txt
Reply With Quote
  #6  
Old 09-17-2006
jambesh's Avatar
Registered User
 

Join Date: Aug 2006
Location: Pune,India
Posts: 118
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-17-2006 at 10:29 PM.
Reply With Quote
  #7  
Old 09-17-2006
jambesh's Avatar
Registered User
 

Join Date: Aug 2006
Location: Pune,India
Posts: 118
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 12:10 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:36 AM.


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

Content Relevant URLs by vBSEO 3.2.0