[완료] 파일내 지정한 라인까지 특정 문자로 채우는 쉘스크립트?

foruses의 이미지

$cat input
3 4 5
1 3 6

아래처럼 총 n번째 줄(가령 n=5), 특정문자(가령 Y)로 채우고 싶습니다. 물론 파일을 열지 않고(몇 행까지 숫자가 있는지 모르는 상태에서), 실행하고 싶습니다.
쉘에서 해결할 수 있을까요? 감사합니다.

$cat output
3 4 5
1 3 6
Y Y Y
Y Y Y
Y Y Y

pynoos의 이미지

awk를 쓰세요

N=5 C="YYY" awk 'BEGIN {L=0} {print $0; L++} END{ for(;L<ENVIRON["N"];L++) { print ENVIRON["C"] } }' input > output
지나가다가있네요.의 이미지

$ cat b.sh
#!/bin/bash
file=$1
n=$2
c=$3
line=$(cat $file | wc -l)
column=$(head -n1 $file | wc -w)
test $line -ge $n && exit
output="$(cat $file)\n"
for((i=line; i for((j=1; j<=column; j++)); do
output="$output$c"
test $j -ne $column && output="$output " || output="$output\n"
done
done
echo -e "$output" | column -t

$ sh b.sh a 4 x

$ sh b.sh a 7 x
1 2 3
4 5 6
7 8 9
10 11 12
x x x
x x x
x x x

지나가다가있네요.의 이미지

$ cat a
1 2 3
4 5 6
7 8 9
10 11 12

지나가다가있네요.의 이미지

for((i=line; i for((j=1; j<=column; j++)); do
가 아닉고

for((i=line; i for((j=1; j<=column; j++)); do
입니다.

foruses의 이미지

감사합니다 ^^

ymir의 이미지

$ n=5
$ cat input && yes $(head -1 input | sed -r 's/[^[:space:]]+/Y/g') | head -$(($n - $(wc -l < input)))
3 4 5
1 3 6
Y Y Y
Y Y Y
Y Y Y

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

지나가다가있네요의 이미지

$ filename=input; n=5; c=Y; echo -e "$(cat a && test $n -gt $(cat $filename | wc -l) && yes $(head -1 $filename | sed "s/[^[:space:]]\+/$c/g") | head -$(($n - $(wc -l < $filename))))" | column -t -o ' '

sed문에서 *가 아니라 \+로 해야겠지요.

ymir의 이미지

옙.. 공백 문자가 2개 이상 들어가 있다면 문제가 생길테니..
* 보다는 \+ 를 쓰는게 좀 더 정확하죠..

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』