쉘 문자열을 cat으로 불러온후 sed로 다른 문자열에 삽입하려는데 안되네요...

enter의 이미지

스크립트에 문자열을 직접 입력해준경우 멀쩡히 잘되는데 파일에서 불러온 문자열은 자꾸 오류가나네요;;

sed에서 구분자도 바꿔보고 sed로 파일을 불러와보기도 했는데 오류나는건 마찬가집니다.

스크립트에 set명령어를 넣고 확인해본 결과 파일을 통해 불러온 문자열은 $'문자열'이런식으로 되있는데 이게 무슨 의미인가요?

만약 이거 때문이라면 해결법 좀 알려주시면 감사하겠습니다.

#변수에 문자열을 직접 넣은 경우
val_insert="123\n456\n789"
var_1="ABC\nDEF\nGHI\nJKL\n"
var_1=$(printf "$var_1" | sed "3 s/$/\n$val_insert/")
printf "$var_1"
 
#실행결과
ABC
DEF
GHI
123
456
789
JKL
 
 
#txt_insert의 내용
123
456
789
 
#파일을 불러와 변수에 넣은 경우
val_insert=$(cat "./txt_insert")
var_1="ABC\nDEF\nGHI\nJKL\n"
var_1=$(printf "$var_1" | sed "3 s/$/\n$val_insert/")
printf "$var_1"
 
#실행결과
sed: -e expression #1, char 11: unterminated `s' command
 
 
#각각의 스크립트에 set명령어를 입력한 후에 확인한 결과
#직접 변수에 넣은 경우
val_insert='123\n456\n789'
#파일을 불러와 변수에 넣은 경우
val_insert=$'123\n456\n789'
김정균의 이미지

문제는,, 처음 테스트 할 때의 val_insert 의 "\n" 문자가 실제 new line 이 아니라 단순히 \n 이라는 string 일 뿐 인거라는 겁니다. 그러니 2 테스트 조건이 달라질 수 밖에 없는 것이고요.

val_insert 를 다음과 같이 \n 을 \\n 으로 변경하여 실제 newline 을 \n 이라는 string 으로 만들어 주면 해결이 됩니다.

val_insert=$(cat "./txt_insert" | sed ':a;N;$!ba;s/\n/\\n/g')
enter의 이미지

'$'가 행의주소로 쓰일때는 마지막줄을 의미하는건가요? 그리고 실제 newline이 있는것과 텍스트에 '\n'이 있는것의 차이는 무엇인가요?

김정균의 이미지

new line 의 ascii 는 0x0a 이고, 문자열에서 사용하는 '\n' 은 0x5c + 0x6e 가 입력이 된거죠.
즉 sed 에서 0x0a 를 처리하지 못한다는 얘기가 됩니다. 그래서 0x0a 를 sed 가 처리해 줄 수 있도록 0x5c0x6e 로 변경을 해 주는 거죠

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.