Bash 如何找到数字序列
我有一个如下格式的数据文件:Bash 如何找到数字序列,bash,shell,Bash,Shell,我有一个如下格式的数据文件: 0.00 0.00 0.00 1 10 1.0 2 12 1.0 3 15 1.0 4 20 0.0 5 23 0.0 0.20 0.15 0.6 1 12 1.0 2 15 1.0 3 20 0.0 4 18 0.0 5 20 0.0 0.001 0.33 0.15 1 8 1.0 2 14 1.0 3 17 0.0 4 25 0.0 5
0.00 0.00 0.00
1 10 1.0
2 12 1.0
3 15 1.0
4 20 0.0
5 23 0.0
0.20 0.15 0.6
1 12 1.0
2 15 1.0
3 20 0.0
4 18 0.0
5 20 0.0
0.001 0.33 0.15
1 8 1.0
2 14 1.0
3 17 0.0
4 25 0.0
5 15 0.0
1 10
1 12
1 8
2 12
2 15
2 14
3 15
3 20
3 17
4 20
4 18
4 25
5 23
5 20
5 15
我需要删除一些数据并按如下方式重新排序行:
0.00 0.00 0.00
1 10 1.0
2 12 1.0
3 15 1.0
4 20 0.0
5 23 0.0
0.20 0.15 0.6
1 12 1.0
2 15 1.0
3 20 0.0
4 18 0.0
5 20 0.0
0.001 0.33 0.15
1 8 1.0
2 14 1.0
3 17 0.0
4 25 0.0
5 15 0.0
1 10
1 12
1 8
2 12
2 15
2 14
3 15
3 20
3 17
4 20
4 18
4 25
5 23
5 20
5 15
我的代码没有显示任何内容。问题可能出在grep命令中。你能帮我一下吗
touch extract_file.txt
for (( i=1; i<=band; i++))
do
sed -e '1, 7d' data_file | grep -w " '$(echo $i)' " | awk '{print $2}' > extract(echo $i).txt
paste -s extract_file.txt extract$(echo $i).txt > data
done
#rm eigen*.txt
touch extract_file.txt
for((i=1;i提取(echo$i).txt
粘贴-s extract_file.txt extract$(echo$i).txt>数据
完成
#rm eigen*.txt
以下带有注释的代码:
cat <<EOF |
0.00 0.00 0.00
1 10 1.0
2 12 1.0
3 15 1.0
4 20 0.0
5 23 0.0
0.20 0.15 0.6
1 12 1.0
2 15 1.0
3 20 0.0
4 18 0.0
5 20 0.0
0.001 0.33 0.15
1 8 1.0
2 14 1.0
3 17 0.0
4 25 0.0
5 15 0.0
EOF
# remove lines not starting with a space
grep -v '^[^ ]' |
# remove leading space
sed 's/^[[:space:]]*//' |
# remove third arg
sed 's/[[:space:]]*[^[:space:]]*$//' |
# stable sort on first number
sort -s -n -k1 |
# each time first number changes, print additional newline
awk '{ if(length(last) != 0 && last != $1) printf "\n"; print; last=$1}'
在perl one liner上测试:
$ perl -lane 'push @{$nums{$F[0]}}, "@F[0,1]" if /^ /;
END { for $n (sort { $a <=> $b } keys %nums) {
print for @{$nums{$n}};
print "" }}' input.txt
1 10
1 12
1 8
2 12
2 15
2 14
3 15
3 20
3 17
4 20
4 18
4 25
5 23
5 20
5 15
$perl-lane'push@{$nums{$F[0]},“@F[0,1]”if/^/;
结束{for$n(排序{$a$b}键%nums){
为{$nums{$n}打印;
打印“}}”input.txt
1 10
1 12
1 8
2 12
2 15
2 14
3 15
3 20
3 17
4 20
4 18
4 25
5 23
5 20
5 15
基本上,对于以空格开头的每一行,使用第一个数字作为哈希表的键,该哈希表存储前两个数字的列表,并按第一个数字排序打印出来。请务必让我们了解在您的帖子中获得预期输出的逻辑。您发布的代码与您所看到的预期输出完全不同另外,
>data
文件data
在每个循环中都被覆盖。$(echo$i)
只是毫无意义,只需删除回显,它只是$i
。sed'1,7d'
删除了前7行,为什么要这样做?grep-w
greps表示单词,但在正则表达式中指定了两个空格和两个”
单引号。如果您能澄清您的要求,我很肯定我们可以在一个程序本身可能使用一个命令。awk'/^/{print$1,$2}file | sort-ns
awk'/^/{print$1,$2}file | sort-ns | awk'NR>1&&prev!=$1{print'}{prev=$1}1'
非常感谢!我还有一个问题。如果所有行都以空格开头,那么我该怎么办?那么这些1
2
行与其他行有何不同?它们不是以整数开头的。