由于 public void update() {
Collections.sort(arrayListBricks, new Comparator<BrickProperties>() {
@Override
public int compare(BrickProperties bp2, BrickProperties bp1)
{
int result = Float.compare(bp1.getTranslateData()[2], bp2.getTranslateData()[2]);
return result;
}
});
Collections.reverse(arrayListBricks);
....
(在bash文件中),我有以下内容:
**grep -e "Full Name" -e "Address" -e "Celphone"|awk '{$1=$1;print}'**
但是,我希望最终输出如下所示:
Full Name: Person A<p>
Address: Address A<p>
Celphone: Celphone A<p>
Full Name: Person B<p>
Address: Address B<p>
Full Name: Person C<p>
Address: Address C<p>
Full Name: Person D<p>
Address: Address D<p>
Celphone: Celphone D<p>
我的挑战是 Celphone 这一行并不总是存在于文件中,而且我不知道如何进行这种&#34;条件连接&#34;。
有人知道怎么做吗?
提前致谢。
答案 0 :(得分:1)
您可以通过awk
命令执行此操作。
awk -F':' '{if(NR==1){printf("%s",$0)}else if($1~"Full Name"){printf("\n%s",$0)} else{printf(",%s",$0)}}' file
它将返回:
Full Name: Person A<p>,Address: Address A<p>,Celphone: Celphone A<p>
Full Name: Person B<p>,Address: Address B<p>
Full Name: Person C<p>,Address: Address C<p>
Full Name: Person D<p>,Address: Address D<p>,Celphone: Celphone D<p>
但是我注意到你的输出中没有<p>
。如果你真的想这样,那么你可以尝试下面的命令:
awk -F':' '{if(NR==1){printf("%s",$0)}else if($1~"Full Name"){printf("\n%s",$0)} else{printf(" , %s",$0)}}' test.txt | sed 's/<p> ,/ ,/g'
它将返回:
Full Name: Person A , Address: Address A , Celphone: Celphone A<p>
Full Name: Person B , Address: Address B<p>
Full Name: Person C , Address: Address C<p>
Full Name: Person D , Address: Address D , Celphone: Celphone D<p>
答案 1 :(得分:1)
这可能适合你(GNU sed):
sed ':a;N;/\nFull Name:/!s/<p>\n/, /;ta;P;D' file
收集完整记录的行,将<p>
和以下换行符替换为,
。然后打印新记录并重复。
答案 2 :(得分:0)
sed -n '/^Full/{x;s/\n/, /g;1!p;d;};H;${g;s/\n/, /g;p;}' filename