解释一下Linux中 awk '$NF == "" {print $2, $1}' annuaire_temp.txt
时间: 2024-02-29 14:55:55 浏览: 174
这是一个awk命令的脚本,它用于处理文本文件annuaire_temp.txt。下面是脚本中各部分的解释:
- awk是一个文本处理命令,用于逐行处理输入文件并根据指定规则进行操作。
- '$NF == "" {print $2, $1}'是awk的操作规则,它包含两部分:
- $NF是awk内置变量,表示每一行的最后一个字段。这里的$NF == ""表示只选择最后一个字段为空的行。
- {print $2, $1}表示对于符合前面条件的每一行,输出该行的第二个字段和第一个字段,中间用空格分隔。
因此,该脚本的作用是从annuaire_temp.txt中选择最后一个字段为空的行,并输出这些行的第二个字段和第一个字段,中间用空格分隔。
相关问题
用Python改写以下代码for i in $(ls ${contact_dir});do #temp: 1acb_E-by #temp1: 1acb_E temp=$(echo ${i%-*}) temp1=$(echo ${temp%-*}) awk '{if(!a[$6]++) print $6}' ${contact_dir}/${i} >${contact_dir}/${temp}-res.txt for j in $(cat ${contact_dir}/${temp}-res.txt);do cat ${contact_dir}/${temp}-res.vor | awk '$6=='$j'{print $0}' >${patch_dir}/${temp}_patch_${j}.txt value=0 for l in $(awk '{print $2}' ${patch_dir}/${temp}_patch_${j}.txt);do #echo $l res=$(awk '{print $2}' ${surface_dir}/${temp1}_surface.txt) #echo $res if ( echo ${res} | grep -q $l);then value=1 fi done #patch_abs for k in $(awk '{print $2}' ${patch_dir}/${temp}_patch_${j}.txt);do cat ${rsa_dir}/${temp1}_abs.txt | awk '$3=='$k'{print $0}' >>${patch_abs_dir}/${temp1}_patch_${j}.txt done for m in $(awk '{print $2}' ${patch_dir}/${temp}_patch_${j}.txt);do for n in $(awk '{print $2}' ${patch_dir}/${temp}_patch_${j}.txt);do if [ $n -ne $m ];then cat ${contact_dir}/${temp}-res.vor | awk '$2=='$m' && $6=='$n' {print $0}' >>${patch_contact_dir}/${temp1}_patch_${j}.txt fi done done #### abs=$(awk '{sum += $4};END {print sum}' ${patch_abs_dir}/${temp1}_patch_${j}.txt) con=$(awk '{sum += $9};END {print sum}' ${patch_contact_dir}/${temp1}_patch_${j}.txt) psaia=$(echo "$abs*$con" | bc) echo $j $abs $con $psaia >>${patch_psaia_dir}/${temp1}.txt #echo $j $con >>${patch_psaia_dir}/${temp1}_con.txt #### #echo $value if [ $value = 0 ];then #echo ${patch_dir}/${temp}_patch_${j} rm ${patch_dir}/${temp}_patch_${j}.txt fi done done
以下是用 Python 重写的代码:
```python
import os
contact_dir = './contact_dir'
patch_dir = './patch_dir'
surface_dir = './surface_dir'
rsa_dir = './rsa_dir'
patch_abs_dir = './patch_abs_dir'
patch_contact_dir = './patch_contact_dir'
patch_psaia_dir = './patch_psaia_dir'
for i in os.listdir(contact_dir):
# temp: 1acb_E-by
# temp1: 1acb_E
temp = i.split('-')[0]
temp1 = temp.split('-')[0]
with open(f'{contact_dir}/{i}') as f:
contacts = f.read().splitlines()
unique_contacts = list(set([c.split()[5] for c in contacts]))
with open(f'{contact_dir}/{temp}-res.txt', 'w') as f:
f.write('\n'.join(unique_contacts))
for j in unique_contacts:
with open(f'{contact_dir}/{temp}-res.vor') as f:
patch_j = [line for line in f if line.split()[5] == j]
value = 0
for l in patch_j:
res = open(f'{surface_dir}/{temp1}_surface.txt').read()
if l.split()[1] in res:
value = 1
# patch_abs
with open(f'{patch_abs_dir}/{temp1}_patch_{j}.txt', 'w') as f:
for k in patch_j:
with open(f'{rsa_dir}/{temp1}_abs.txt') as f2:
patch_abs = [line for line in f2 if line.split()[2] == k.split()[1]]
f.write('\n'.join(patch_abs))
# patch_contact
with open(f'{patch_contact_dir}/{temp1}_patch_{j}.txt', 'w') as f:
for m in patch_j:
for n in patch_j:
if n.split()[1] != m.split()[1]:
with open(f'{contact_dir}/{temp}-res.vor') as f2:
patch_contact = [line for line in f2 if line.split()[1] == m.split()[1] and line.split()[5] == n.split()[5]]
f.write('\n'.join(patch_contact))
# psaia
abs_sum = sum(float(line.split()[3]) for line in open(f'{patch_abs_dir}/{temp1}_patch_{j}.txt'))
con_sum = sum(float(line.split()[8]) for line in open(f'{patch_contact_dir}/{temp1}_patch_{j}.txt'))
psaia = abs_sum * con_sum
with open(f'{patch_psaia_dir}/{temp1}.txt', 'a') as f:
f.write(f'{j} {abs_sum} {con_sum} {psaia}\n')
if value == 0:
os.remove(f'{patch_dir}/{temp}_patch_{j}.txt')
```
注意,这里只是根据原始代码的语法和意思进行的重写,可能还需要根据实际情况进行调整。
awk '{if(length($NF) != 10) print $1,$2}' annuaire_sed.txt
您的问题是一个 Linux 的命令行问题,这是一条使用 awk 命令的命令,它的作用是从文件 annuaire_sed.txt 中读取数据,并将最后一列的长度不为10的行的第一列和第二列打印出来。具体解释如下:
- awk:命令本身,用于处理文本文件并生成报告。
- '{if(length($NF) != 10) print $1,$2}':命令的操作部分,其中 $NF 表示最后一列,length 函数用于计算字符串的长度,如果最后一列的长度不等于 10,则打印第一列和第二列。
- annuaire_sed.txt:输入文件的名称。
因此,这个命令会从文件 annuaire_sed.txt 中读取数据,并打印出最后一列的长度不为 10 的行的第一列和第二列。
阅读全文