如果Linux中的任何其他列中都有零,那么如何使除第一列之外的整行为零?
我想将除第一列之外的整行置零,如果它在任何其他列中有零值。e、 g如果Linux中的任何其他列中都有零,那么如何使除第一列之外的整行为零?,linux,bash,shell,unix,sed,Linux,Bash,Shell,Unix,Sed,我想将除第一列之外的整行置零,如果它在任何其他列中有零值。e、 g ifile.txt 1 4.5 9 2 5.0 0 3 2.4 4 4 3.1 2 5 0.0 0 6 2.4 1 7 0.0 5 I am looking my output as ofile.txt 1 4.5 9 2 0.0 0 3 2.4 4 4 3.1 2 5 0.0 0 6 2.4 1 7 0.0 0
ifile.txt
1 4.5 9
2 5.0 0
3 2.4 4
4 3.1 2
5 0.0 0
6 2.4 1
7 0.0 5
I am looking my output as
ofile.txt
1 4.5 9
2 0.0 0
3 2.4 4
4 3.1 2
5 0.0 0
6 2.4 1
7 0.0 0
假设输入文件中的列由选项卡分隔:
awk -F'\t' '{ if ($2 == 0 || $3 == 0) { $2 = 0; $3 = 0 }; printf("%d\t%.1f\t%d\n", $1, $2, $3) }' ifile.txt
输出:
1 4.5 9
2 0.0 0
3 2.4 4
4 3.1 2
5 0.0 0
6 2.4 1
7 0.0 0
1 4.5 3.2 .5 1.9 2.0 9
2 0.0 0.0 .0 0.0 0.0 0
3 2.4 4.1 .4 2.3 1.0 4
4 3.1 2.0 .6 1.3 2.4 2
5 0.0 0.0 .0 0.0 0.0 0
6 2.4 2.0 .1 4.5 1.2 1
7 0.0 0.0 .0 0.0 0.0 0
8 0.0 0.0 .0 0.0 0.0 0
使用bash(和bc)和任意数量的列:
#!/bin/bash
IFS=$'\n'
for line in $(cat in.txt)
do
found_zero=
zeros=
IFS=$' '
# Find if zero in any column, and generate columns with zeros
for e in ${line#* }
do
(( $(bc <<< "$e == 0.0") == 1 )) && found_zero=1
zeros="$zeros 0.0"
done
# If found zero in a column, write only zeros
if [ $found_zero ]
then
echo "${line%% *}$zeros"
else
echo $line
fi
done
#/bin/bash
IFS=$'\n'
对于以美元为单位的行(cat in.txt)
做
找到零=
零=
IFS=$“”
#在任何列中查找是否为零,并生成带零的列
对于${line#*}中的e
做
($(公元前
带格式化列
sed 's/$/ /;/[[:space:]]\(0\.\)\{0,1\}0[[:space:]]/!b o
h;s/[[:space:]].*//;x;s/[^[:space:]]*//;s/[1-9]/0/g;H;x;s/\n//
:o
s/ $//' YourFile
这在保留空白的同时实现了您想要的结果:
gawk '!($2*$3) {$0 = gensub(/([[:space:].])[0-9]/,"\\10", "g")} 1' ifile.txt
说明:
- 条件:
!($2*$3)
:如果第二个或第三个字段之一为零(0),则其乘积为零
- 如果条件为真,则将空格或点之后的所有数字替换为零(0)
\\10
:实际上是\1
+0
:意思是打印第一个匹配项(空格或点),后跟文字零(0)
如果要使其适用于任意数量的字段,例如以下输入:
$ cat ifile_ext.txt
1 4.5 3.2 .5 1.9 2.0 9
2 5.0 6.4 .1 0.0 3.4 0
3 2.4 4.1 .4 2.3 1.0 4
4 3.1 2.0 .6 1.3 2.4 2
5 0.0 1.0 .9 2.3 0.0 0
6 2.4 2.0 .1 4.5 1.2 1
7 0.0 4.5 .2 9.4 0.0 5
8 1.0 2.0 .0 1.2 4.1 1
然后必须在字段上循环,然后执行相同的转换:
gawk '{ for(i=2;i<=NF;i++) if (!$i) {$0 = gensub(/([[:space:].])[0-9]/,"\\10", "g")} } 1' ifile_ext.txt
您不必指定FS
,默认值为任何空格。
gawk '{ for(i=2;i<=NF;i++) if (!$i) {$0 = gensub(/([[:space:].])[0-9]/,"\\10", "g")} } 1' ifile_ext.txt
1 4.5 3.2 .5 1.9 2.0 9
2 0.0 0.0 .0 0.0 0.0 0
3 2.4 4.1 .4 2.3 1.0 4
4 3.1 2.0 .6 1.3 2.4 2
5 0.0 0.0 .0 0.0 0.0 0
6 2.4 2.0 .1 4.5 1.2 1
7 0.0 0.0 .0 0.0 0.0 0
8 0.0 0.0 .0 0.0 0.0 0