Viewed k times. Improve this question. There if no efficient way to do that for large files: unix. Add a comment. Active Oldest Votes. Linux : echo 'task goes here' cat - todo. Improve this answer. Lanre 3 2 2 bronze badges. Dennis Williamson Dennis Williamson Something else must have done that. Instead of cat , try piping into hexdump -C to see if you're actually sending backslash and n or if it's a newline.
You could also try cat -e to show line endings. Using 2 and 3 3 seems simpler to me allows you to prepend text to many files at once. Kira: The 1 means do the next command only on line one of the file and the i command is insert. Look in the man page under the "Addresses" section and in the "Zero- or One- address commands" section.
Show 13 more comments. Sasha 78 2 2 silver badges 16 16 bronze badges. John Alberts John Alberts 1, 7 7 silver badges 2 2 bronze badges. That might work better or at all with double quotes instead of single… — raphink. Won't the -e also convert escape sequences inside todo. Show 3 more comments. The moreutils have a nice tool called sponge : echo "task goes here" cat - todo. I would go for echo 'ble'; cat todo. I would use tee instead of sponge which come by default on most distro echo "task goes here" cat - todo.
The tee example is not correct askubuntu. Now I remember why I didn't suggest it originally. If you want your script to be portable, you should stick to POSIX specified features whenever possible. You set the file variable in your for loop to the name of each file in turn assuming no special characters or whitespace in the filenames, which will cause the file variable to contain something which is not a filename , but you never actually use the file variable.
Your Sed command is not given any file to run on, so it will attempt to run on standard input. Thus when you run the script it will simply wait for input. Your Sed script itself is incorrect, independent of the fact that it's missing a filename to operate on. I see that you escaped only the last slash, which should not be escaped. There is a little-known feature in Sed , which you could use to your advantage here.
Any character other than a backslash or newline can be used as a regex delimiter, rather than only using a slash. Now if you've handled all of the above points, you will have a working script. It may not do what you want it to do, but it will actually do something. Such a script would look like this:. What does this script do? It searches the current directory recursively for all files with the executable bit set for the owner , and for each such file, prints the entire file with the text Hello World appended after any lines which contain the text!
Sed is not actually designed for editing files in place; it is the Stream EDitor. GNU Sed will allow you to edit files in place using the -i switch, but I would just use the standard tool ex for file editing. But there is another point here. If you want to add the line Hello World in a Bash script, it won't actually do anything, as Hello is not a valid command name.
Perhaps what you want is to print the text "Hello World" in the Bash script, in other words to add echo "Hello World , which could make sense. Sign up to join this community. The best answers are voted up and rise to the top. Active Oldest Votes. Fredrik Pihl Fredrik Pihl Community Bot 1 1 1 silver badge. Gilles Quenot Gilles Quenot k 33 33 gold badges silver badges bronze badges. This is the correct way to solve the problem. Add the -i to edit the file.
Note to OP: this still uses a temporary file, but it at least hides the details from you. It won't be any faster, though. See these other SO questions for further details: Prepending Data to a file Is there a file system with a low level prepend operation At a higher level, even if that operation is supported by the FS driver, it is still possible that programs don't use the feature.
I guess my answer isn't much of use for your problem, but it might help understanding the reason behind the symptoms. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Note that although Bash or many other shells do not have a limit on the size of a variable, however, the size may be limited based on the environment and system configuration.
I test this in bash on Linux Mint A better and safer solution: Create a new temp file. Write the new text to the start of the new temp file.
Append the full text from the original file to the new temp file. Delete the original file, or better still, rename the original file to a backup filename. Rename the temp filename to the original filename. Hi Rick, Thanks for the suggestion. We will include a note to advise the user to backup the file first. I think you should mention it. Hi Michael, You are right, there could be a limit on the size of variables.
Got something to say?
0コメント