bash - Append or write to file using here-strings -
how can write file or append string file using ed only?
i have knowledge of other editors particular form of writing in bash script ed confuses me lot:
ed filename <<< $'a texttowriteinfile\nwq'
the previous line not work and, although have read ed man pages, still confused here-strings
method. not interested in here-document
method.
edit:
i've tried $ed h myfile <<< $'a\nmy line here\n.\nwq' h option , error 'h: no such file or directory'. have created file named myfile , sudo chmod a+wx myfile in directory.
tl;dr:
ed myfile <<< $'a\nmy line here\n.\nwq'
a sad truth programming can never automate don't know how manually. if don't know how append line manually ed
, can't hope append line automatically through ed
, here-string.
the first step therefore how append lines in ed
. here's info ed
:
the sample sessions below illustrate basic concepts of line editing 'ed'. begin creating file, 'sonnet', shakespeare. shell, input 'ed' must followed character. comments begin '#'.
$ ed # 'a' command appending text editor buffer. no more grieved @ thou hast done. roses have thorns, , filvers foutians mud. clouds , eclipses stain both moon , sun, , loathsome canker lives in sweetest bud. . # entering single period on line returns 'ed' command mode. # write buffer file 'sonnet' , quit: w sonnet 183 # 'ed' reports number of characters written. q
ok, let's adapt append single line file , quit:
$ touch myfile $ ed myfile text here . wq
and let's verify worked:
$ cat myfile text here
yay. we're able manually append line, have recreate same input here-string. can use cat
verify our input correct:
$ cat <<< $'a\nmy line here\n.\nwq' line here . wq
yup, input used. can plug ed
:
$ echo "existing contents" > myfile $ ed myfile <<< $'a\nmy line here\n.\nwq' 18 31 $ cat myfile existing contents line here
Comments
Post a Comment