문제 설명
공백이 많은 줄을 읽고 출력에서 유지 (Reading lines with a lot of spaces and maintaining them in output)
다음과 같은 파일이 있지만 훨씬 더 큽니다.
xxxxxxx xxxxx xxxx
yyyyyyy yyyyy yyyy
zzzzzzz zzzzz zzzz
bbbbbbb bbbb
하지만 훨씬 더 큽니다. 두 번째 열과 세 번째 열을 비교하고 싶습니다. 처음 4자리만 작성했습니다. 다음과 같이 :
while IFS='' read ‑r line; do
a=${line:181:4}
b=${line:276:4}
str1="$a"
str2="$b"
# echo $a
# echo $b
# echo $str1
# echo $str2
if [[ "$str1" == "$str2" ]]
then
echo $line >> $1.diffs.txt
fi
done <$1
str*의 이유는 숫자가 때때로 0으로 시작하여 8진수로 해석하여 오류가 발생하기 때문입니다. 제 문제는 $1.diffs.txt의 출력이 모든 공백 대신 1개의 공백만 포함합니다. IE
출력:
xxxxxxx xxxxx xxxx
yyyyyyy yyyyy yyyy
zzzzzzz zzzzz zzzz
참조 솔루션
방법 1:
You could use awk
in place of your existing shell script:
awk '{ if (substr($2,0,4) == substr($3,0,4)) { print $0 }}' $1 > $1.diffs.txt
awk
separates on whitespaces by default, so you can do your comparison with the variables $2
and $3
instead of having to manually define the substring positions