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

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

anbu23,
awk's InputFieldSeparator variable is 'FS'.
shell's InputFieldSeparator variable is 'IFS'.

Woah, I didn't know FOO=BAR="value" was legal in awk. Good to know!

temp=2 ;temp1=1 ;temp3=3
awk 'BEGIN{OFS=FS="\t"} {print "'"$temp"'" " " "'"$temp1"'" " " "'"$temp3"'" }' client_data.txt

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.

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 ??????

Jambesh try this one.....here jam1.txt is lookup file and jam2.txt is data file needs to be rearranged.you need to create temp directory in directory where you will run this script.May be this will help...and jamt will be the output file

maxcol=`sed '1q' jam1.txt | awk -F" " '{print NF}'`
col_look=`sed '1q' jam1.txt`
col_seq=`sed '1q' jam2.txt`
set -A look $col_look
set -A seq $col_seq
set -A pos

i=0
while [ $i -lt $maxcol ]
  do
   j=0
   while [ $j -lt $maxcol ]
   do
   [[ "${look[$i]}" = "${seq[$j]}" ]] && pos[$i]=`expr $j + 1` 
   j=`expr $j + 1`
  done
i=`expr $i + 1`
done

k=0
while [ $k -lt $maxcol ]
do
cut -d ' ' -f${pos[$k]} jam2.txt>temp/tempjam${k}  
 k=`expr $k + 1`
done

paste temp/tempjam* >jamt
rm -f temp/tempjam*

p.s.
output file columns will be tab delimeter if you want to make it space delimited replace "paste command with

paste -d ' ' temp/tempjam* >jamt

Dhruv,

Here is my jam2.txt file

cat jam2.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

jam1.txt contain look up column

MANDT SERAIL SERSCHA SEREX EQTYP BSTVP

when i run your script it produce jamt but upon cat the file
i got the out put in a garbled form not readable

out put of jam1.txt should be jampt and it should contain
MANDT SERAIL SERSCHA SEREX EQTYP BSTVP
510 hsgdfs
510 bg
510 gh

this way ...but i got the result like ...

MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98 510 bg 89 fg 23 98 510 bg 89 fg 23 98 510 bg 89 fg 23 98 510 bg 89 fg 23 98 510 bg 89 fg 23 98
510 gh 89 we sew mn 510 gh 89 we sew mn 510 gh 89 we sew mn 510 gh 89 we sew mn 510 gh 89 we sew mn 510 gh 89 we sew mn

unreadable ------please assist me asap im in stuck last two days

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}

what is the problem with this two line ??? i could not get ? any idea to correct it so that it will print the desired column ????

i have checked with the below files and found output file jamt.Please see i had updated my post with paste command.you remove rm command in script and check what is going into tempjam files.
jam1.txt:::

col1 col2 col3 col4
11 22 33 44
111 222 333 444

jam2.txt::::

col3 col4 col2 col1
33 44 22 11
333 444 222 111
3 4 2 1

jamt::::

col1 col2 col3 col4
11 22 33 44
111 222 333 444
1 2 3 4

Dhruv ,
Here is sample out put of each of temjam0 , tempjam1 tempjam2,....
tempjam0 tempjam1 tempjam2 tempjam3 tempjam4 tempjam5
$ cat tempjam0
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam1
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam2
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam3
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam4
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam5
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn
sequence is not changing in any of the file ...what is wrong ?
i guess each of the file should produce only one column ..right ?? but it is containing the whole file again surprise !!!!!!!!!!!!!!

yes only one column in each file.problem here is with cut command.
run the below commands on command line one by one and check what is going in files.The field delimeter is single space.
Make sure there is a single space in ' ' (single quotes in below command)

cut -d ' ' -f1 jam2.txt>temp/tempjam0
cut -d ' ' -f2 jam2.txt>temp/tempjam1
cut -d ' ' -f3 jam2.txt>temp/tempjam2

dhruv,
You are cutting data using cut -d ' ' ??? it is a space delimeter but i think it should be tab delimited ??
is it the problem please assist me.

i checked and written this script with assumption that field delimeter is space.
if your file has different delimeter then make the changes.
if delimeter is tab then use cut without -d option.

Try...

awk -F '\t' 'FNR==1{split($0,a)}{for(x=1;x<=NF;x++)print $x>a[x]}' jam2.txt
paste $(<jam1.txt) > jam2.txt

dhruv,
i have tried with cut -d '\t' and also with cut -d "\t"
it is saying invalid delimeter !! is there any other way out ! my real problem is here boss !

Yogor thanks for replay
i tried with your code as well
but it also giving me error.
awk: syntax error near line 1
awk: bailing out near line 1
paste: cannot open MANDT

---I am completely frustrated with this problem don't understand what to do here now plz assist.

I want to just print the selected column from a file which is delimited by tab and not space .
if you can explain me why
awk -v c1=$f1 -v c2=$f2 'BEGIN{FS=OFS="\t"}{print $c1,$c2}' jam2.txt
this is not working it will be of great help to me

jambesh tab is default delimiter and you don't need to use -d option

cut -f1 jam2.txt>temp/tempjam0

or

cut -d 'tab' -f1 jam2.txt>temp/tempjam0

here press tab key in between single quotes.

Try using nawk or gawk.

Dhruv,
Thanks a lot for help , thanks for guiding me all through my problem too.

when i left -d option it said invalid range also when i give a tab in between ' ' and trying it give
cut: invalid character in range . i don't understand acttualy if default is tab delimeter why it is not accepting .

also when i am doing cut -f1 jam2.txt > one.txt
all the field of file jam2.txt is copying to one.txt instead of just column one

Real surprise !!!!! for me

please buddy guide me i am completely frustrated and bitting my head on desk for it

Yogor !!!

i have tried with nawk and gawk too nothing happend boss!