1

I have this C shell (unfortunately C is required) script called EditFile.script consisting entirely of 6 different sed commands, all to be applied to one text file.

#!/bin/csh -f

1i GameTime!!!!!!\n #append "GameTime!!!!!! at the beginning
s/pionts/points/g #sub typo
s/shot/shoot/g #sub typo
s/the\sthe/the/g #sub double the's with "the"
/^ *$/d #delete all blank lines
$a Game\nOver #append "Game Over" to the end of the file

Now the text file is 400+ lines long and I think posting it here (and its desired form) would be a waste of space.

However, when I run sed -f EditFile.script GameManual at the cmdline all of the above commands work except for the final command. No "Game Over" is appended to the end of the file. I believe it has something to do the with preceding "delete all blank lines /^ *$/d command- because when I ran the script with it removed, "Game Over" was appended. But I don't see the connection.

Why will the final sed command in the script not show? Also, since this is my first time using a csh script (and c shell), what other additions must I make for the script to work?

  • 1
    If these are sed commands (executed with sed -f), it's not really a "C shell script" and you can remove the #!/bin/csh -f (which is just a comment as far as sed is concerned). Not sure if it makes a difference, but what (interactive) shell are you running the sed command from? – steeldriver Oct 23 '16 at 17:15
  • @steeldriver C shell. – KM142646 Oct 23 '16 at 17:57
2

I don't think this has anything to do with C shell - and in fact, as noted in the comments, what you have is a sed script (a simple text file of sed commands) rather than a shell script, and the #!/bin/csh -f shebang is superfluous and will be treated (by sed) as a simple comment.

What you seem to be observing is that sed appears to not re-evaluate the $ (last line) address, so that if one of your previous commands happens to delete the last line of the file, then a subsequent attempt to append1 after that address fails. We can see that with a simple test (using the bash shell, for what it's worth) as follows:

# delete line 3 then append after $
$ printf 'line %d\n' {1..3} | sed -e '/3/d' -e '$a\new line'
line 1
line 2

whereas

# append after $ then delete line 3
$ printf 'line %d\n' {1..3} | sed -e '$a\new line' -e '/3/d'
line 1
line 2
new line

I find this somewhat surprising but don't know if it's the desired behavior or should be considered a bug.


Note:

[1] Techically, the a command doesn't append, but rather

Queue[s] the lines of text which follow [ ... ] to be output at the end of the current cycle, or when the next input line is read.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.