simple scripting question
I have a file I want to edit with a script. I want to change the first three lines (I can rewrite them completely).
I know I can use tail +4 filetoedit to get that file without the first 3 lines, but what can I use to attach some new lines to it (I know how to dump it back to the file using > filetoedit). |
How about:
echo -e "New line 1 \n New line 2 \n New Line 3" >newfile; tail --lines=+4 filetoedit >> newfile ; [ and maybe: mv newfile filetoedit ] Dave |
that should do it, I almost had it with:
line1="stuff" line2="stuff2" line3="stuff3" oldfile=`tail +4 ztext` echo $line1 > ztext echo $line2 >> ztext echo $line3 >> ztext echo -e $oldfile >> ztext But this would dump the old file into the new file without any linebreaks (I thought -e would take care of that) |
give an example, what do you have AND what do you want.
|
davidstvz,
Assigning each line to a variable is fine for one or two lines, but obviously is quite impractical for larger problems. And you don't want to try to assign large files to a shell variable through command substitution. There are better methods. You could use shell here documents. Try this: Code:
{ tail +4 ztext; Or you could do it via sed, perl, awk, etc. to perform the job more efficiently. |
Code:
cat replace3.sed Using a HERE document IMHO is a better way of doing it because the lines can be entered literally (no \) allowing easy editing. You can use it to Code:
#!/usr/bin/env bash |
Quote:
You can simply do this: Code:
(cat <<-"END" Yves. |
Quote:
|
All times are GMT -5. The time now is 11:39 PM. |