>

다음 파일이 있습니다

   more /home/list.in
    master01.fsdns.com AMBARI_METRICS STARTED
    master02.fsdns.com AMBARI_METRICS STARTED
    master03.fsdns.com AMBARI_METRICS STARTED
    worker01.fsdns.com AMBARI_METRICS STARTED
    worker02.fsdns.com AMBARI_METRICS STARTED
    worker03.fsdns.com AMBARI_METRICS STARTED
    worker05.fsdns.com AMBARI_METRICS STARTED
    worker06.fsdns.com AMBARI_METRICS STARTED
    worker07.fsdns.com AMBARI_METRICS STARTED
    worker08.fsdns.com AMBARI_METRICS STARTED
    worker09.fsdns.com AMBARI_METRICS STARTED
    master01.fsdns.com YARN STARTED
    master02.fsdns.com YARN STARTED
    master03.fsdns.com YARN STARTED
    worker01.fsdns.com YARN STARTED
    worker02.fsdns.com YARN STARTED
    worker03.fsdns.com YARN STARTED
    worker05.fsdns.com YARN STARTED
    worker06.fsdns.com YARN STARTED
    worker07.fsdns.com YARN STARTED
    worker08.fsdns.com YARN STARTED
    worker09.fsdns.com YARN STARTED
    master01.fsdns.com HDFS STARTED
    master02.fsdns.com HDFS STARTED
    master03.fsdns.com HDFS STARTED
    worker01.fsdns.com HDFS STARTED
    worker02.fsdns.com HDFS STARTED
    worker03.fsdns.com HDFS STARTED
    worker05.fsdns.com HDFS STARTED
    worker06.fsdns.com HDFS STARTED
    worker07.fsdns.com HDFS STARTED
    worker08.fsdns.com HDFS STARTED
    worker09.fsdns.com HDFS STARTED

파일의 순서를 바꾸고 싶다-list.in을 다음과 같은 구조로 (예상 결과)

기계 번호와 관련된 모든 줄은 같은 그룹에 있습니다

예상 결과

   master01.fsdns.com AMBARI_METRICS STARTED
    master01.fsdns.com YARN STARTED
    master01.fsdns.com HDFS  STARTED
    master02.fsdns.com AMBARI_METRICS STARTED
    master02.fsdns.com YARN STARTED
    master02.fsdns.com HDFS STARTED
    master03.fsdns.com AMBARI_METRICS STARTED
    master03.fsdns.com YARN STARTED
    master03.fsdns.com HDFS STARTED
    .
    .
    .
    .
    . 
    worker09.fsdns.com AMBARI_METRICS STARTED
    worker09.fsdns.com YARN STARTED
    worker09.fsdns.com HDFS STARTED

지금까지 시도한 것

for i in 01 02 03 04 05 06 07 
 do
  grep  worker$i /tmp/list.in
 done

 worker01.fsdns.com AMBARI_METRICS STARTED
 worker01.fsdns.com YARN STARTED
 worker01.fsdns.com HDFS STARTED
 worker02.fsdns.com AMBARI_METRICS STARTED
 worker02.fsdns.com YARN STARTED
 worker02.fsdns.com HDFS STARTED
 worker03.fsdns.com AMBARI_METRICS STARTED
 worker03.fsdns.com YARN STARTED
 worker03.fsdns.com HDFS STARTED
 worker05.fsdns.com AMBARI_METRICS STARTED
 worker05.fsdns.com YARN STARTED
 worker05.fsdns.com HDFS STARTED
 worker06.fsdns.com AMBARI_METRICS STARTED
 worker06.fsdns.com YARN STARTED
 worker06.fsdns.com HDFS STARTED
 worker07.fsdns.com AMBARI_METRICS STARTED
 worker07.fsdns.com YARN STARTED
 worker07.fsdns.com HDFS STARTED

  • 답변 # 1

    $ sort -k1,1 list.in  | 
        awk '
          /^[[:space:]]*$/ { next };
          lasthost == "" { lasthost = $1 };
          $1 == lasthost { print $0; next };
          {print "\n" $0 ; lasthost=$1 }' 
    master01.fsdns.com AMBARI_METRICS STARTED
    master01.fsdns.com HDFS STARTED
    master01.fsdns.com YARN STARTED
    master02.fsdns.com AMBARI_METRICS STARTED
    master02.fsdns.com HDFS STARTED
    master02.fsdns.com YARN STARTED
    master03.fsdns.com AMBARI_METRICS STARTED
    master03.fsdns.com HDFS STARTED
    master03.fsdns.com YARN STARTED
    worker01.fsdns.com AMBARI_METRICS STARTED
    worker01.fsdns.com HDFS STARTED
    worker01.fsdns.com YARN STARTED
    worker02.fsdns.com AMBARI_METRICS STARTED
    worker02.fsdns.com HDFS STARTED
    worker02.fsdns.com YARN STARTED
    worker03.fsdns.com AMBARI_METRICS STARTED
    worker03.fsdns.com HDFS STARTED
    worker03.fsdns.com YARN STARTED
    worker05.fsdns.com AMBARI_METRICS STARTED
    worker05.fsdns.com HDFS STARTED
    worker05.fsdns.com YARN STARTED
    worker06.fsdns.com AMBARI_METRICS STARTED
    worker06.fsdns.com HDFS STARTED
    worker06.fsdns.com YARN STARTED
    worker07.fsdns.com AMBARI_METRICS STARTED
    worker07.fsdns.com HDFS STARTED
    worker07.fsdns.com YARN STARTED
    worker08.fsdns.com AMBARI_METRICS STARTED
    worker08.fsdns.com HDFS STARTED
    worker08.fsdns.com YARN STARTED
    worker09.fsdns.com AMBARI_METRICS STARTED
    worker09.fsdns.com HDFS STARTED
    worker09.fsdns.com YARN STARTED
    
    

    awk 스크립트는 $1 필드에 표시된 마지막 호스트 이름을 추적하고 변경 될 때마다 현재 입력 행 앞에 줄 바꿈을 인쇄합니다. 또한 완전히 비어 있거나 공백 문자 만 포함하는 행은 건너 뜁니다.

    첫 번째 레코드 전에 빈 줄이 인쇄되지 않도록하기 위해 lasthost 도 확인합니다.  변수가 비어 있고 (즉, 정의되지 않음) 변수가 있으면 설정합니다.

  • 답변 # 2

    빈 줄이 중요하지 않은 경우 간단한 정렬 명령은 다음과 같습니다.

    sort -t. -k1 /home/list.in
    
    

    (빈 행으로 표시) :

    master01.fsdns.com AMBARI_METRICS STARTED
    master01.fsdns.com HDFS STARTED
    master01.fsdns.com YARN STARTED
    master02.fsdns.com AMBARI_METRICS STARTED
    master02.fsdns.com HDFS STARTED
    master02.fsdns.com YARN STARTED
    master03.fsdns.com AMBARI_METRICS STARTED
    master03.fsdns.com HDFS STARTED
    master03.fsdns.com YARN STARTED
    worker01.fsdns.com AMBARI_METRICS STARTED
    worker01.fsdns.com HDFS STARTED
    worker01.fsdns.com YARN STARTED
    worker02.fsdns.com AMBARI_METRICS STARTED
    worker02.fsdns.com HDFS STARTED
    worker02.fsdns.com YARN STARTED
    worker03.fsdns.com AMBARI_METRICS STARTED
    worker03.fsdns.com HDFS STARTED
    worker03.fsdns.com YARN STARTED
    worker05.fsdns.com AMBARI_METRICS STARTED
    worker05.fsdns.com HDFS STARTED
    worker05.fsdns.com YARN STARTED
    worker06.fsdns.com AMBARI_METRICS STARTED
    worker06.fsdns.com HDFS STARTED
    worker06.fsdns.com YARN STARTED
    worker07.fsdns.com AMBARI_METRICS STARTED
    worker07.fsdns.com HDFS STARTED
    worker07.fsdns.com YARN STARTED
    worker08.fsdns.com AMBARI_METRICS STARTED
    worker08.fsdns.com HDFS STARTED
    worker08.fsdns.com YARN STARTED
    worker09.fsdns.com AMBARI_METRICS STARTED
    worker09.fsdns.com HDFS STARTED
    worker09.fsdns.com YARN STARTED
    
    

  • 답변 # 3

    이 작동합니다 :

    awk '$1{a[$1];b[$2]}
    END{asorti(a);for( i in a){for(j in b){printf("%s %s\n",a[i],j)};printf("\n")}}' file
    
    

    $1  비어 있지 않은 첫 번째 필드의 경우
    와이즈 비즈  배열 a와 b를 만듭니다.
    와이즈 비즈  모든 파일을 읽은 후
    와이즈 비즈  배열을 정렬 와이즈 비즈  각 기계마다
    와이즈 비즈  각 서버마다
    와이즈 비즈  정렬 된 값을 인쇄합니다.
    와이즈 비즈  빈 줄 하나를 인쇄하십시오.
    와이즈 비즈  입력 파일 용.

  • 답변 # 4

    Awk와 sed를 사용하여 동일한 결과를 얻었습니다. 테스트 한 결과 제대로 작동합니다

    {a[$1];b[$2]}
    
    

    출력

    END{
    
    
    asorti(a)

관련 자료

  • 이전 bash - 중괄호가 잘못 지정된 mkdir -p dir
  • 다음 ubuntu - LM 센서의 칩 이름을 얻는 방법