Hi...
I am trying extract data using 2 files..
- Data file - DATA.TXT containing data to be extracted
- Column file - LIST.TXT header column to be extracted
output not coming properly..
if the header field name matches with data file header extract data...this actually I wanted ...
#!/bin/bash
DATAFILE=${1:-DATA.TXT}
COLUMNFILE=${2:-LIST.TXT}
awk -F, -v colsFile="$COLUMNFILE" '
BEGIN {
j=1
while ((getline < colsFile) > 0) {
col[j++] = $1
}
n=j-1;
close(colsFile)
for (i=1; i<=n; i++) s[col]=i
}
NR==1 {
for (f=1; f<=NF; f++)
if ($f in s) c[s[$f]]=f
next
}
{ sep=""
for (f=1; f<=n; f++) {
printf("%c%s",sep,$c[f])
sep=FS
}
print ""
}
' "$DATAFILE"
cat DATA.TXT
ID, head1, head2, head3, head4
1, 25.5, 1364.0, 22.5, 13.2
2, 10.1, 215.56, 1.15, 22.2
cat LIST.TXT
ID
head1
head4
sh extract.sh DATA.TXT LIST.TXT
1,1, 25.5, 1364.0, 22.5, 13.2,1, 25.5, 1364.0, 22.5, 13.2
2,2, 10.1, 215.56, 1.15, 22.2,2, 10.1, 215.56, 1.15, 22.2
and I want to display it as space separated ..
awk '{print $3,$2,$1}' FS="," DATA.TXT
whether here some changes can be done to get desired output
please help..
awk 'NR==1{for(i=1;i<=NF;i++)if($i~/ID/)f[n++]=i}{for(i=0;i<n;i++)printf"%s%s",i?" ":"",$f;print""}' DATA.TXT
I could able to get output from above script but, I have so many columns, how can I search, and display as space or tab separated.