grep
출력에서 특정 값을 얻기 위해 또는 다른 도구를 사용하는 방법
255.00
아래 출력에서 다음과 같은 값을 가져와야 합니다 .Minimum: 255.00 (1.0000)
다음과 같은 패턴:Channel Statistics:\s+Gray:\s+Minimum: +([\d.]+)
Image: test.tif
Format: TIFF (Tagged Image File Format)
Geometry: 2525x1785
Class: DirectClass
Type: bilevel
Depth: 1 bits-per-pixel component
Channel Depths:
Gray: 1 bits
Channel Statistics:
Gray:
Minimum: 255.00 (1.0000)
Maximum: 255.00 (1.0000)
Mean: 255.00 (1.0000)
Standard Deviation: 0.00 (0.0000)
Filesize: 581
Interlace: No
Orientation: Unknown
Background Color: white
Border Color: #DFDFDF
Matte Color: #BDBDBD
답변1
Perl을 사용하면 다음을 수행할 수 있습니다. minimum:
블록 내부의 숫자 값을 캡처하여 Channel Statistics:
인쇄합니다.
perl -0 -ne '/Channel Statistics:\s+Gray:\s+Minimum:\h+([\d.]+)/ && print $1,"\n"' file
산출:(주어진 예를 들어)
255.00
설명:
-0 # specifies the input record separator. If there are no digits, the null character is the separator. The whole file is read in a single string.
-n # Iterate over the file
-e # execute the command line
정규식:
/ # regex delimiter
Channel Statistics: # literally
\s+ # 1 or more any kind of spaces
Gray: # literally
\s+ # 1 or more any kind of spaces
Minimum: # literally
\h+ # 1 or more horizontal spaces
( # start group 1
[\d.]+ # 1 or more digit or dot
) # end group
/ # regex delimiter
답변2
와 함께sed
sed -rn 's/^\s+Minimum:\s+([0-9.]+).+$/\1/p' image.data
느린 속도로:
-r
sed
확장된 정규 표현식 "구문"을 사용한다고 알려줍니다 .-n
sed
일치하지 않는 행을 인쇄하지 말라고 지시합니다 .s/^\s+Minimum:\s+([0-9.]+).+$/\1/
목표 라인과 일치하고 이를 원하는 값으로 대체합니다.p
sed
결과를 인쇄하라고 알려준다
이전 줄의 내용을 고려하여 명확하게 해야 하는 경우에는 약간 더 복잡합니다.
sed -r ':a;N;$!ba; s/^.*Gray:\s*\n\s+Minimum:\s+([0-9.]+).+$/\1/' image.data
어디:
:a;N;$!ba;
sed
전체 파일을 한 번에 로드하는 언어 의 루프입니다 .-n
인쇄하고 싶지 않은 다른 줄이 없으므로 더 이상 필요하지 않습니다.- 우리가 사용하지 않기 때문에 최종은
p
더 이상 필요하지 않습니다.-n
답변3
입력에서 'Minimum:' 문자열이 정확히 한 번 발생한다고 가정하면 이는 매우 단순합니다.
awk '/Minimum:/ {print $2}'