如何通过awk判断关键词?

时间:2019-06-06 01:45:30

标签: shell awk

我的项目中有一个名为tmpInfo.txt的工作信息文件。 tmpInfo.txt的内容如下:

875675 mpirun -machinefile $PBS_NODEFILE -np $NP /opt/software/vasp/vasp-intel/vasp5.4/vasp5.4.1/vasp_std>o.$PBS_JOBID.out
875676 mpirun -machinefile $PBS_NODEFILE -np $NP /opt/software/vasp/vasp-intel/vasp5.4/vasp5.4.1/vasp_std>o.$PBS_JOBID.out
875683 source /opt/software/Gaussian/g09-env.sh
875695 
875696 
875697 EXEC=/home/inspur/tensorflow_test/test.py python3 $EXEC
875698 /opt/intel/impi/5.0.2.044/intel64/bin/mpirun -machinefile $PBS_NODEFILE -n 480 /opt/software/vasp/vasp-intel/vasp5.4/vasp5.4.1/vasp_gam
875699 fluent 3d -g -t$proc -mpi=intel -ssh -cnf=conf_file -i aircraft_wing_2m.commands
875764 source /opt/software/Gaussian/g09-env.sh /opt/software/Gaussian/g09/g09 <Bush_2_b_fix1a_1_fre_ts1a3_irc_r_opt1_sccc_fix1_ts_irc_r_opt_fsc.gjf>> "Bush_2_b_fix1a_1_fre_ts1a3_irc_r_opt1_sccc_fix1_ts_irc_r_opt_fsc.log"

现在我想根据每一行的关键词得出结论;

我尝试过:

awk '{if ($0=~"Gaussian") print $0" "gaussion; else if ($0=~"fluent") print $0" "fluent; else print $0" "NKOWN}' tmpInfo.txt

但不幸的是,我收到了错误信息:

awk: {if ($0=~"Gaussian") print $0" "gaussion; else if ($0=~"fluent") print $0" "fluent; else print "C"}
awk:         ^ syntax error
awk: {if ($0=~"Gaussian") print $0" "gaussion; else if ($0=~"fluent") print $0" "fluent; else print "C"}
awk:                                           ^ syntax error
awk: {if ($0=~"Gaussian") print $0" "gaussion; else if ($0=~"fluent") print $0" "fluent; else print "C"}
awk:                                                       ^ syntax error
awk: {if ($0=~"Gaussian") print $0" "gaussion; else if ($0=~"fluent") print $0" "fluent; else print "C"}
awk:

这似乎没什么问题,谁能帮助我?

2 个答案:

答案 0 :(得分:1)

您需要对代码进行2个主要更改。第一次更改=~(在shell中使用,awk不是shell)更改为~/string/,然后我认为gaussion是要在输出中打印的文本,因此请换行"gaussion"中也是如此,同样适用于其他文本。

尝试关注一次。

awk '{if($0~/Gaussian/){print $0" gaussion"} else if($0~/fluent/){print $0" fluent"} else{print $0" UNKOWN"}}'  Input_file

或如下所示的非一个内衬形式的解决方案。

awk '
{
  if($0~/Gaussian/){
     print $0" gaussion"
  }
  else if($0~/fluent/){
     print $0" fluent"
  }
  else{
     print $0" UNKOWN"
  }
}'  Input_file

答案 1 :(得分:0)

假设您没有高斯和流利的字符同时出现的行,并且您真的不想在输出中误拼高斯,我可以使用GNU awk做到这一点:

awk '{print $0, (match{$0,/Gaussian|fluent/,a) ? tolower(a[0]) : "UNKNOWN")}'  Input_file

或任何awk:

awk '{print $0, (match{$0,/Gaussian|fluent/) ? tolower(substr($0,RSTART,RLENGTH)) : "UNKNOWN")}'  Input_file