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

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 』

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.