how to merge similar lines in linux

2020-04-26 02:53发布

I have a file test.txt on my linux system which has data in following format :

first second third fourth 10  
first second third fourth 20  
fifth sixth seventh eighth 10  
mmm nnn ooo ppp 10  
mmm nnn ooo ppp 20   

I need to modify the format as below -

first second third fourth 10 20  
fifth sixth seventh eighth 10 0  
mmm nnn ooo ppp 10 20  

I have tried following code

cat test.txt | sed 'N;s/\n/ /' | awk -F" " '{if ($1~$5){print $1" "$2" "$3" "$4" "$8} else { print $0 }}'

But this is not giving required output. When there is a line which doesn't have a similar line below it,this command fails. Can u suggest me any solution for this??

3条回答
Summer. ? 凉城
2楼-- · 2020-04-26 03:05

Reuse of my solution (J4F)

cat file.txt | sort | while read L;
do
  y=`echo $L | rev | cut -f2- -d' ' | rev`;
  {
    test "$x" = "$y" && echo -n " `echo $L | awk '{print $NF}'`";
  } || 
  {
    x="$y";echo -en "\n$L"; 
  };
done
查看更多
男人必须洒脱
3楼-- · 2020-04-26 03:13

Here is one way to do it:

awk ' {
  last=$NF; $NF=""
  if($0==previous) {
    tail=tail " " last
  }
  else {
    if(previous!="") {
      if(split(tail,foo)==1) tail=tail " 0"
      print previous tail
    }
    previous=$0
    tail=last
  }
}
END {
    if(previous!="") print previous tail
}
'
查看更多
等我变得足够好
4楼-- · 2020-04-26 03:17

Perl solution:

perl -ne '/^(.*) (\S+)/ and push @{ $h{$1} },$2 }{ print "$_ @{$h{$_}}\n" for keys %h' < test.txt
查看更多
登录 后发表回答