2

If I want to add content to the 10th line of a file, how do I do it?

This is what I came up with:

sed -ie '' "10s/^/new_content/g" file.txt

I keep getting the message "no such file or directory"

Also, if I want to replace 10 with N+1 and the new_content with a variable $VAR, would the format still be the same?

VAR= $(cat fileA.txt)
sed -ie '' "`expr $N +1`s/^/$VAR/g" fileB.txt

Thanks for your help!!!

ryankenner
  • 33
  • 3
  • `sed -ie` means that `sed` will create backup files for you by adding the extension `e` to the original file name. Not formally wrong, but pretty implausible. Your empty string `''` is then an empty `sed` script; this is followed by a 'file name` such as `3/^/something/g` which probably doesn't exist and then by `final.txt`. Shell has a perfectly workable `elif` which should be used. The semicolons after `fi` are unnecessary. It would help no end if you include a pair of short sample inputs (the original inputs, which appear to be `length.txt` and `final.txt`, plus the expected output. – Jonathan Leffler Jul 13 '15 at 03:28
  • Sorry, I removed the original code so that I could stick with the original context of the thread. I will try elif to see what happens. – ryankenner Jul 13 '15 at 03:31
  • `elif` is (was) stylistic, not substantive. Instead of `else if ... then; ...; fi; fi;` you can use `elif ... then; ...; fi` — it cuts down the number of `fi` and the amount of indentation. – Jonathan Leffler Jul 13 '15 at 03:32
  • Thanks for the sed -ie comment. I was not trying to make a backup. I was trying to call -i and -e. – ryankenner Jul 13 '15 at 03:44
  • On Mac, the `-i` option demands a backup suffix, which can be an empty string: `-i.bak` or `-i .bak` or `-i ''` (but not `-i''`). Using `-i -e '…'` would give you backups with extension `-e`. Note that GNU `sed` has different semantics for `-i`; using `-i.bak` gives you backups with `.bak` extension; `-i` on its own means backups without extensions. If the script is to work on both Mac and GNU `sed`, you must use `-i.bak` to make the script work reliably (where the spelling of `.bak` is of your choosing, but must be present and non-empty). (I mean it: the shell _is_ fussy about spacing). – Jonathan Leffler Jul 13 '15 at 03:47
  • possible duplicate of [Using SED -n with variables in a script](http://stackoverflow.com/questions/31374269/using-sed-n-with-variables-in-a-script) – l'L'l Jul 13 '15 at 04:05

2 Answers2

0

Shell is fussy about spacing:

VAR= $(cat fileA.txt)

You do not want the space after the =. Actually, you don't want the assignment either, unless fileA.txt has only one line. You'd have to escape newlines with backslashes, etc, to get it to work, which is both hard and unnecessary.

Unless you're using an antique (non-POSIX) shell, use built-in arithmetic:

sed -i.bak -e "$(($N + 1))r fileA.txt" fileB.txt

The $(($N + 1)) has the shell evaluate the arithmetic expression. The r has sed read the file that is named.

[1addr]r file
Copy the contents of file to the standard output immediately before the next attempt to read a line of input. If file cannot be read for any reason, it is silently ignored and no error condition is set.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

It turns out I needed to split -i and -e.

The final result that works was:

        sed -i '' -e "10s/^/CONTENT/g" file.txt

Or to increase Ns by +1 each time it loops:

        sed -i '' -e "$((N + 1))s/^/CONTENT/g" ~/dance/heatlist/prep.txt

I still have not figured out how to make CONTENT a variable $CONTENT

ryankenner
  • 33
  • 3
  • 1
    Please don't use back-ticks, and not only because they're difficult to handle in Markdown in comments. And don't use `expr` in modern code. – Jonathan Leffler Jul 13 '15 at 03:56