Bash 基于两个文件的条件+列匹配生成新文件
首先,如果我在发帖前没有看到类似的答案,我要道歉 我正试图根据几个条件创建第三个文件 我有两个输入文件 文件1选项卡分隔:-Bash 基于两个文件的条件+列匹配生成新文件,bash,if-statement,awk,sed,while-loop,Bash,If Statement,Awk,Sed,While Loop,首先,如果我在发帖前没有看到类似的答案,我要道歉 我正试图根据几个条件创建第三个文件 我有两个输入文件 文件1选项卡分隔:- X_ID1 y_id11 num1 X_ID2 y_id31 num2 X_ID3 y_id34 num3 X_ID4 y_id23 num4 X_ID5 y_id2 num5 ... ... 文件2:- BIOTIC AND ABIOTIC STRESS x_id2 REGULATION OF TRANSCRIPTION x_id1 x_id4 HO
X_ID1 y_id11 num1
X_ID2 y_id31 num2
X_ID3 y_id34 num3
X_ID4 y_id23 num4
X_ID5 y_id2 num5
...
...
文件2:-
BIOTIC AND ABIOTIC STRESS
x_id2
REGULATION OF TRANSCRIPTION
x_id1
x_id4
HORMONES
x_id5
REGULATION
x_id6
x_id13
...
...
****请注意,文件1的第1列为大写,文件2中的数据为小写
我想要的是这样一个输出文件file3,如下所示:-
BIOTIC AND ABIOTIC STRESS
y_id31
REGULATION OF TRANSCRIPTION
y_id11
y_id23
HORMONES
y_id2
...
...
while read $line from file2; do
if [[line1 != x_*]]; then
print $line
else
match $line (case insensitively) with column 1 of file1 and print respective column2 of file1
fi
done
基本上,如果我想到一个伪代码,它如下所示:-
BIOTIC AND ABIOTIC STRESS
y_id31
REGULATION OF TRANSCRIPTION
y_id11
y_id23
HORMONES
y_id2
...
...
while read $line from file2; do
if [[line1 != x_*]]; then
print $line
else
match $line (case insensitively) with column 1 of file1 and print respective column2 of file1
fi
done
你能帮我解决这个问题吗
提前多谢 在awk中:
$ awk 'NR==FNR{a[tolower($1)]=$2;next}{print ($1 in a?a[$1]:$0)}' file1 file2
BIOTIC AND ABIOTIC STRESS
y_id31
REGULATION OF TRANSCRIPTION
y_id11
y_id23
HORMONES
y_id2
REGULATION
x_id6
x_id13
解释:
$ awk '
NR==FNR { # first file
a[tolower($1)]=$2 # hash to a, key is lowercase $1 data is $2
next # skip tp next record
}
{ # second file
print ($1 in a?a[$1]:$0) # if $1 exists in hash a, print it, else print current
}' file1 file2 # mind the order
根据@Sundeep的建议,这是对awk中两个文件处理的一个很好的介绍。在awk中:
$ awk 'NR==FNR{a[tolower($1)]=$2;next}{print ($1 in a?a[$1]:$0)}' file1 file2
BIOTIC AND ABIOTIC STRESS
y_id31
REGULATION OF TRANSCRIPTION
y_id11
y_id23
HORMONES
y_id2
REGULATION
x_id6
x_id13
OLD_IFS="${IFS}"
IFS=$'\n'
for line in `cat file2`
do
if [[ -z `echo "${line}" | grep x_*` ]]
then
echo "${line}"
else
grep -i "${line}" file1 | awk -F ' ' '{print $2}'
fi
done
IFS="${OLD_IFS}"
解释:
$ awk '
NR==FNR { # first file
a[tolower($1)]=$2 # hash to a, key is lowercase $1 data is $2
next # skip tp next record
}
{ # second file
print ($1 in a?a[$1]:$0) # if $1 exists in hash a, print it, else print current
}' file1 file2 # mind the order
根据@Sundeep的建议,这是对awk中两个文件处理的一个很好的介绍。可以通过一个while循环完成:-
OLD_IFS="${IFS}"
IFS=$'\n'
for line in `cat file2`
do
if [[ -z `echo "${line}" | grep x_*` ]]
then
echo "${line}"
else
grep -i "${line}" file1 | awk -F ' ' '{print $2}'
fi
done
IFS="${OLD_IFS}"
while IFS= read -r line;
do
var=`echo $line | tr '[a-z]' '[A-Z]'`
col2=`grep "$var" file1|cut -d" " -f2`
if [[ -z "$col2" ]] ; then
echo "$line" >> file3
else
echo "$col2" >> file3
fi
done < file2
说明:-
var=echo$line | tr'[a-z]'[a-z]'-将小写转换为大写
col2=grep$var file1 | cut-d-f2-匹配file1中的模式。如果不匹配,即变量col2为空,则将行写入文件file3,否则将col2写入文件
可通过一个while循环完成:-
while IFS= read -r line;
do
var=`echo $line | tr '[a-z]' '[A-Z]'`
col2=`grep "$var" file1|cut -d" " -f2`
if [[ -z "$col2" ]] ; then
echo "$line" >> file3
else
echo "$col2" >> file3
fi
done < file2
说明:-
var=echo$line | tr'[a-z]'[a-z]'-将小写转换为大写
col2=grep$var file1 | cut-d-f2-匹配file1中的模式。如果不匹配,即变量col2为空,则将行写入文件file3,否则将col2写入文件
充满了坏习惯。。。看,谢谢你。是的,它确实有效。然而,我更喜欢awk的答案,因为它非常简单。但根据我的标签,你的很好用充满了坏习惯。。。看,谢谢你。是的,它确实有效。然而,我更喜欢awk的答案,因为它非常简单。但根据我的标签,你的很好用非常感谢你的解释。当您能够理解解释中的内容而不必盲目地运行代码时,这是非常棒的。是的,它起作用了!:非常感谢你的解释。当您能够理解解释中的内容而不必盲目地运行代码时,这是非常棒的。是的,它起作用了!: