bash脚本循环两个函数

bash脚本循环两个函数

我想按最高值对第五列进行排序,如果第四列与第五列匹配,则选择第四列。

猫表列表

C1 C2  C3          C4                                          C5 C6
3  No  ENCRYPTION  /opt/oracle/oradata/ORCLCDB/encryption.dbf  8  0  
2  No  RETENTION   /opt/oracle/oradata/ORCLCDB/retention.dbf   5  0  
4  No  ORACLE      /opt/oracle/oradata/ORCLCDB/oracle.dbf      2  0 
1  No  USERS       /opt/oracle/oradata/ORCLCDB/users01.dbf     3  0`

它应该看起来像这样。

for file in $C5; do
select $C4 from from dual;
done


select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;`

到目前为止我所尝试的方法是有效的,但它根据 C1 的值读取输出,但我想根据 C5 从最高到最低读取输出。

for (( x=1; x <= ${FILE_COUNT}; x++)) ; do
        FILE_NAME=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x {print $4} $3 == x {print $1}'
        FILE_SIZE=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x {print $5} $3 == x {print $1}'
done

谢谢!

答案1

根据评论您想要按照第 5 列指定的顺序列出第 4 列(以相反的数字顺序)。

$ sed '1d' <TABLE_LIST | sort -k5,5nr | awk '{ print $4 }'
/opt/oracle/oradata/ORCLCDB/encryption.dbf
/opt/oracle/oradata/ORCLCDB/retention.dbf
/opt/oracle/oradata/ORCLCDB/users01.dbf
/opt/oracle/oradata/ORCLCDB/oracle.dbf

sed命令从文件中删除标题,并按sort数字降序对第五列上的剩余行进行排序。最后awk提取第四列。

只要列本身不包含空白字符,这种方法就可以工作。


您想插入这些selectfrom dual;字符串吗,然后您可以修改awk管道的部分:

$ sed '1d' <TABLE_LIST | sort -k5,5nr | awk '{ printf("select %s from dual;\n", $4) }'
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;

答案2

使用 GNU Awk > 4.0

gawk '
  FNR>1 {a[$5] = $4} 
  END {
    PROCINFO["sorted_in"] = "@ind_num_desc"; 
    for (i in a) print "select " a[i] " from dual;"
  }
' TABLE_LIST 
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;

相关内容