![grep 결과에 공백과 특수 문자가 포함되어 있습니다.](https://linux55.com/image/204903/grep%20%EA%B2%B0%EA%B3%BC%EC%97%90%20%EA%B3%B5%EB%B0%B1%EA%B3%BC%20%ED%8A%B9%EC%88%98%20%EB%AC%B8%EC%9E%90%EA%B0%80%20%ED%8F%AC%ED%95%A8%EB%90%98%EC%96%B4%20%EC%9E%88%EC%8A%B5%EB%8B%88%EB%8B%A4..png)
lshw에서 결과를 가져와 bash 배열에 추가하여 새 문자열을 만들려고 합니다. 나는 사용하고있다
lshw -class disk |egrep -A 7 .'-d' |grep 'product' |cut -b 17-
출력은 다음과 같습니다
Samsung SSD 850
Samsung SSD 840
ST8000AS0002-1NA
ST8000AS0002-1NA
Samsung SSD 870
Samsung SSD 870
ST8000VN0022-2EL
Flash Drive FIT
Flash Drive FIT
ST8000AS0002-1NA
ST8000VN0022-2EL
Samsung SSD 870
Samsung SSD 870
ST8000VN004-2M21
이 출력을 bash 배열에 추가하려고 시도했지만 공백으로 구분된 단어의 각 줄이 배열의 요소가 됩니다. 이 명령의 결과를 볼 수도 있습니다.
for w in $(lshw -class disk |egrep -A 7 .'-d' |grep 'product' |cut -b 17-); do printf "$w \n" ;done
산출
Samsung
SSD
850
Samsung
SSD
840
ST8000AS0002-1NA
ST8000AS0002-1NA
Samsung
SSD
870
Samsung
SSD
870
ST8000VN0022-2EL
Flash
Drive
FIT
Flash
Drive
FIT
ST8000AS0002-1NA
ST8000VN0022-2EL
Samsung
SSD
870
Samsung
SSD
870
ST8000VN004-2M21
공백으로 구분된 줄을 문자열로 유지하는 방법은 무엇입니까?
답변1
mapfile
아마도 가장 간단 할 것입니다.
mapfile -t myarray < <(lshw -class disk |egrep -A 7 .'-d' |grep 'product' |cut -b 17-)
그러나 리터럴 배열을 사용하지 않고 결과만 반복하려는 경우 다음을 수행할 수 있습니다.
lshw -class disk |egrep -A 7 .'-d' |grep 'product' |cut -b 17- | while read w ; do [something with w] ; done
답변2
인용 명령 대체:
# for w in "$(lshw -class disk |egrep -A 7 .'-d' |grep 'product' |cut -b 17-)"; do printf "$w \n" ;done
USB3.0 DISK00
USB3.0 DISK01
USB3.0 DISK02
MS/MS-Pro
SD/MMC
Compact Flash
SM/xD-Picture
ST1000DM003-1CH1
WDC WD20EZAZ-00G
for
그러면 "USB3.0 DISK00" "USB3.0 DISK01"... 이 루프 에 전달됩니다 .