Hi
I have a file in the following format:
ftrn facc ttrd feed xref
#1 1365 1366 1367 1097 1098
#2 1368 1369 1370 1095 1096
#3 1371 1372 1373 1065 1066
#4 1374 1375 1376 1099 1100
#5 1377 1378 1379 1080 1081
#6 1380 1381 1382 1101 1102
#7 1383 1384 1385 1331 1332
etc....
Is there a way/commands to retrieve the number which is located on the cross of let's say #5 and feed , which is 1080. May be somebody did something similar before. Thanks a lot for any help or advice -A
correction the format of the file looks like that:
ftrn facc ttrd feed xref
#1 1365 1366 1367 1097 1098
#2 1368 1369 1370 1095 1096
#3 1371 1372 1373 1065 1066
#4 1374 1375 1376 1099 1100
#5 1377 1378 1379 1080 1081
#6 1380 1381 1382 1101 1102
#7 1383 1384 1385 1331 1332
etc....
Sorry, the the file looks like that:
.....ftrn facc ttrd feed xref
#1 1365 1366 1367 1097 1098
#2 1368 1369 1370 1095 1096
#3 1371 1372 1373 1065 1066
#4 1374 1375 1376 1099 1100
#5 1377 1378 1379 1080 1081
#6 1380 1381 1382 1101 1102
#7 1383 1384 1385 1331 1332
etc....
you have FOUR entries in 'header': "ftrn facc ttrd feed xref"
and you have FIVE fields per line/record.
What's missing?
Are the leading '#' (#1 , #2 , etc) really in a file?
Please use vB Codes when posting data and/or code samples.
joeyg
February 12, 2009, 3:53pm
5
> cat file168
ftrn facc ttrd feed xref
#1 1365 1366 1367 1097 1098
#2 1368 1369 1370 1095 1096
#3 1371 1372 1373 1065 1066
#4 1374 1375 1376 1099 1100
#5 1377 1378 1379 1080 1081
#6 1380 1381 1382 1101 1102
#7 1383 1384 1385 1331 1332
> cat find168.sh
#! /usr/bin/bash
ROWV="5"
COLV="feed"
FILEIN="file168"
# determine the column to match $COLV
COLP=`head -1 ${FILEIN} | tr " " "\n" | cat -n | grep "${COLV}" | awk '{print $1}'`
COLP=`echo $COLP+1 | bc`
# extract from the row $ROWV
grep "^#${ROWV}" ${FILEIN} | cut -d" " -f ${COLP}
> find168.sh
1080
Assuming input file (ao.txt) looks like:
ftrn facc ttrd feed xref
1365 1366 1367 1097 1098
1368 1369 1370 1095 1096
1371 1372 1373 1065 1066
1374 1375 1376 1099 1100
1377 1378 1379 1080 1081
1380 1381 1382 1101 1102
1383 1384 1385 1331 1332
To get the 5-th row and the column under the 'feed' heading:
nawk -v row=5 -v col='feed' -f ao.awk ao.txt
ao.awk:
FNR==1 { for(i=1; i<=NF;i++) header[$i]=i; next }
FNR==row+1 { print $header[col]; exit }
#!/usr/bin/perl
sub get{
my($file,$line,$col)=(@_);
open FH,"<$file";
while(<FH>){
chomp;
if($.==1){
my @temp=split(" ",$_);
for($i=0;$i<=$#temp;$i++){
if($col eq $temp[$i]){
$num=$i;
last;
}
}
}
if($.==$line+1){
my @temp=split(" ",$_);
print $temp[$num+1],"\n";
}
}
}
get("a.txt",5,"feed");