如何获得两种不同输入(sed和nawk)的一些输出

如何获得两种不同输入(sed和nawk)的一些输出,awk,sed,nawk,Awk,Sed,Nawk,我有许多输入文件。以下是两个输入文件示例: 输入文件#1: 输入文件#2: 正如您所注意到的,两个输入文件之间的唯一区别是第二组数字与输入#2的顺序相反(点数是-5而不是5) 我想要的输出文件与两者相同: -124.087 41.52 -124.092 41.54 -124.098 41.56 -124.103 41.58 -124.109 41.60 -122.050 41.60 -122.060 41.57 -122.056 41.55 -122.052 41.53 -122.041 41.

我有许多输入文件。以下是两个输入文件示例:

输入文件#1:

输入文件#2:

正如您所注意到的,两个输入文件之间的唯一区别是第二组数字与输入#2的顺序相反(点数是-5而不是5)

我想要的输出文件与两者相同:

-124.087 41.52
-124.092 41.54
-124.098 41.56
-124.103 41.58
-124.109 41.60
-122.050 41.60
-122.060 41.57
-122.056 41.55
-122.052 41.53
-122.041 41.52
(本质上是输入#2减去定义的点数(10和-10))

这对于输入#2来说很简单(解决方案:nawk'{if(NF>1)print$0}'Input.txt>output.txt)。 但是,我需要一个或一组命令,将按顺序打印第一组5个数字,然后如果以下整数为正(例如5),则按相反顺序打印以下5个点,或者如果整数为负(例如-5),则按与输入文件相同的顺序打印以下点

我尝试了sed、nawk和tac命令的组合来尝试并完成这个任务。我无法让它工作。有什么提示吗

原始问题发布后的其他并发症:

好的,如果第二组数字之前的整数是正的,我想把第二组数字倒过来。第一组数字的顺序是保持不变的,不管原始问题是什么。安德烈·马库哈在下面提供了一个完美的解决方案,但我实际上需要用一个更详细的问题来复杂化这个问题

如果第一组数字为负数,我希望将第一组数字反转。

假设我的输入文件(#3)是:

#3的期望输出:

更新:安德烈的解决方案(第2部分)有效!

awk 'NF==1 { s++; rev=($1>0)?1:0; next; }
s==2 && rev { a[++i] = $0; next; }
1
END { while(i>0) print a[i--]; }' input.txt
测试版本为20121220

如果希望代码反转这两个部分:

awk '
NF==1 {                               # single element: new section encountered
    while(i>0) print a[i--];          # print out previous section in reverse (if needed)
    s++;                              # increment section counter
    pos = ($1>0) ? 1 : 0;             # single number is positive?
    next;                             # proceed to next line
}
s==2 && pos || s==1 && !pos {         # are we in "reversing state"? if yes:
    a[++i] = $0;                      # remember this line in array
    next;                             # proceed to next line 
}
1                                     # else: print the line as is
END { 
    while(i>0) print a[i--];          # print out previous section in reverse (if needed)
}' input.txt

数字
5
是固定的还是可以是任何其他数字,如
12
345
?如果脚本仅在第二个数字为负时反转第二个块(如果为正则保持不变),是否可以接受?Melpome,是的,这是我想要的。=-)Sundeep,数字5不是固定的(应该提到这一点)安德烈·马库哈,这正好解决了我的问题。但是,如果我还想切换第一组数字的顺序,如果该数字是负数,该怎么办。我将尝试使用这个命令并使其工作,但我并不真正理解命令组件100%。=/。顺便说一句,谢谢在最初的问题发布后,在“额外的复杂性”下添加了一些细节:“该死的,你在我的大得多的文件(1000个分数)上进行了快速测试,看起来很有效。谢谢
-5
-124.109 41.60
-124.103 41.58
-124.098 41.56
-124.092 41.54
-124.087 41.52
-3
-122.050 41.60
-122.056 41.55
-122.041 41.52
-124.087 41.52
-124.092 41.54
-124.098 41.56
-124.103 41.58
-124.109 41.60
-122.050 41.60
-122.056 41.55
-122.041 41.52
awk 'NF==1 { s++; rev=($1>0)?1:0; next; }
s==2 && rev { a[++i] = $0; next; }
1
END { while(i>0) print a[i--]; }' input.txt
awk '
NF==1 {                               # single element: new section encountered
    while(i>0) print a[i--];          # print out previous section in reverse (if needed)
    s++;                              # increment section counter
    pos = ($1>0) ? 1 : 0;             # single number is positive?
    next;                             # proceed to next line
}
s==2 && pos || s==1 && !pos {         # are we in "reversing state"? if yes:
    a[++i] = $0;                      # remember this line in array
    next;                             # proceed to next line 
}
1                                     # else: print the line as is
END { 
    while(i>0) print a[i--];          # print out previous section in reverse (if needed)
}' input.txt