comm 커맨드를 실행하여 두 파일의 내용을 비교한다. 


$ cat > file.txt

1

2

$ cat > file2.txt


2


-1 매개 변수는 비교해서 첫번째 파일에만 있는 것은 출력하지 않는다. 

$ comm -1 file1.txt file2.txt


2


-2 매개 변수는 비교해서 두번째 파일에만 있는 것은 출력하지 않는다. 

$ comm -2 file1.txt file2.txt

1

2



-3 매개 변수는 비교해서 모두 존재하는 라인은 출력하지 않는다. 



$ comm -3 file1.txt file2.txt


1



comm 커맨드의 -3 매개 변수를 이용한 활용예시


$ comm -3 <(docker ps -a -q --filter=status=exited | sort)  <(docker ps -a -q --filter=exited=0 | sort) |  xargs --no-run-if-empty docker inspect 




Posted by '김용환'
,



트위터의 Future.rescure 함수가 마음에 들 때가 있다. (간결함이 좋다..)




https://twitter.github.io/util/docs/com/twitter/util/Future.html를 참조하면. 예외가 발생할 때 새로운 결과로 이어지게 한다. 코드가 간결해 진다. 


def rescue[B >: A](rescueException: PartialFunction[Throwable, Future[B]]): Future[B]

If this, the original future, results in an exceptional computation, rescueException may convert the failure into a new result.


import com.twitter.util.{Await, Future}
val f1: Future[Int] = Future.exception(new Exception("boom1!"))
val f2: Future[Int] = Future.exception(new Exception("boom2!"))
val newf: Future[Int] => Future[Int] = x => x.rescue {
	case e: Exception if e.getMessage == "boom1!" => Future.value(1)
}
Await.result(newf(f1)) // 1
Await.result(newf(f2)) // throws java.lang.Exception: boom2!




실제 예.


https://github.com/rodrigodealer/finagle_bootstrap/blob/master/src/main/scala/com/github/rodrigodealer/FacebookService.scala#L25

 def getToken : Future[Option[FacebookToken]] = {

    val request = GetRequest(s"/oauth/access_token?client_id=$apiKey&client_secret=$apiSecret&grant_type=client_credentials",

      "graph.facebook.com", true)

    perform(request) flatMap { token =>

      token.status match {

        case Ok => Future(Option(fromJson[FacebookToken](token.contentString)))

        case _ => Future(Option.empty)

      }

    } rescue { case _ => Future(Option.empty)}

  }



자바의 Future에는 rescue대신 exceptionally가 있다. 예쁘지 않다... 



AtomicBoolean thenAcceptCalled = new AtomicBoolean(false);

        AtomicBoolean exceptionallyCalled = new AtomicBoolean(false);

        CompletableFuture<String> future = new CompletableFuture<>();

        future.thenAccept(value -> {

            thenAcceptCalled.set(true);

        }).exceptionally(throwable -> {

            exceptionallyCalled.set(true);

            return null;

        });

        future.completeExceptionally(new RuntimeException());

        assertThat(thenAcceptCalled.get(), is(false));

        assertThat(exceptionallyCalled.get(), is(true));



Posted by '김용환'
,



elasticsearch에 terminate_after 라는 매개변수가 있다. 리눅스 커맨드로 따지면, 'head/tail -n 1 file.txt | wc -l '정도 될 것 같다. 


데이터가 있는지 먼저 확인한다.


// http://localhost:9200/_search?q=name:Andrew


{

  "took": 29,

  "timed_out": false,

  "_shards": {

    "total": 1,

    "successful": 1,

    "skipped": 0,

    "failed": 0

  },

  "hits": {

    "total": {

      "value": 1,

      "relation": "eq"

    },

    "max_score": 0.2876821,

    "hits": [

      {

        "_index": "company",

        "_type": "_doc",

        "_id": "AOmqCWwB3Ptn8KUa8r_2",

        "_score": 0.2876821,

        "_source": {

          "name": "Andrew",

          "age": 45,

          "experienceInYears": 10

        }

      }

    ]

  }

}




terminate_after는 size 매개변수와 함께 사용된다.



못찾으면 다음과 같은 결과가 나온다.

// http://localhost:9200/_search?q=name:kimchi&size=0&terminate_after=1


{

  "took": 841,

  "timed_out": false,

  "terminated_early": false,

  "_shards": {

    "total": 1,

    "successful": 1,

    "skipped": 0,

    "failed": 0

  },

  "hits": {

    "total": {

      "value": 0,

      "relation": "eq"

    },

    "max_score": null,

    "hits": [

      

    ]

  }

}

hits.total.value의 값은 0이다. 



찾으면 아래와 같은 결과가 나온다. 


// http://localhost:9200/_search?q=name:Andrew&size=0&terminate_after=1


{

  "took": 13,

  "timed_out": false,

  "terminated_early": false,

  "_shards": {

    "total": 1,

    "successful": 1,

    "skipped": 0,

    "failed": 0

  },

  "hits": {

    "total": {

      "value": 1,

      "relation": "eq"

    },

    "max_score": null,

    "hits": [

      

    ]

  }

}


hits.total.value의 값이 1이다. 



size=0의 의미는 검색 결과에 관심이 없음을 의미한다. terminate_after=1은 검색 결과에 맞는 결과가 1개가 나타나면 더 이상 검색하지 않고 종료된다.




Posted by '김용환'
,


도커의 모든 컨테이너를 삭제하는 커맨드이다. 


docker ps -a -q |  xargs docker rm -f




docker ps-a -q의 경과가 빈다면 파이프 다음 커맨드를 실행하고 싶지 않다면

--no-run-if-empty를 추가한다.




docker ps -a -q |  xargs --no-run-if-empty docker rm -f


Posted by '김용환'
,