![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
awk -v temp=2 -v temp1=1 -v temp3=3 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt
|
|
#3
|
||||
|
||||
|
Quote:
awk's InputFieldSeparator variable is 'FS'. shell's InputFieldSeparator variable is 'IFS'. |
|
#4
|
||||
|
||||
|
Quote:
|
|
#5
|
|||
|
|||
|
Code:
temp=2 ;temp1=1 ;temp3=3
awk 'BEGIN{OFS=FS="\t"} {print "'"$temp"'" " " "'"$temp1"'" " " "'"$temp3"'" }' client_data.txt
|
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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. |
||||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|