LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   simple scripting question (https://www.linuxquestions.org/questions/linux-newbie-8/simple-scripting-question-665011/)

davidstvz 08-24-2008 10:03 AM

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).

david1941 08-24-2008 10:14 AM

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

davidstvz 08-24-2008 10:25 AM

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)

vikas027 08-24-2008 11:32 PM

give an example, what do you have AND what do you want.

Mr. C. 08-25-2008 02:18 AM

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;
  cat <<EOF
new line 1
new line 2
new line 3
EOF
} > ztext.tmp
mv ztext.tmp ztext

You could also read the file in a shell loop, outputting each line except the first 4, and appending your extra lines.

Or you could do it via sed, perl, awk, etc. to perform the job more efficiently.

jschiwal 08-25-2008 05:10 AM

Code:

cat replace3.sed
1i\
line1\
line2\
line3
1,3d

sed -f bvs.sed file >newfile

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
TEMPFILE=/tmp/TEMP$$

cat >$TEMPFILE << EOF
line1
line2
line3
EOF
sed '1,3d' file >>$TEMPFILE
mv $TEMPFILE file

I used /tmp/TEMP$$ to create a temporary file to reduce the change of filename collision. Another trick when using C is to delete a temporary file just after opening it. The file isn't deleted until it is closed. This way if the program aborts prematurely the file will be deleted.

theYinYeti 08-25-2008 05:58 AM

Quote:

Originally Posted by davidstvz (Post 3257933)
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)

That's because $line1, $line2, $line3, and $oldfile aren't enclosed in quotes (").

You can simply do this:
Code:

(cat <<-"END"
line 1…
line 2…
line 3…
END
tail +4 input) >output

The quotes around END on the first line ensure that dollars won't be treated specially (as shell variables).

Yves.

davidstvz 08-25-2008 08:58 AM

Quote:

Originally Posted by theYinYeti (Post 3258846)
That's because $line1, $line2, $line3, and $oldfile aren't enclosed in quotes (").

You can simply do this:
Code:

(cat <<-"END"
line 1…
line 2…
line 3…
END
tail +4 input) >output

The quotes around END on the first line ensure that dollars won't be treated specially (as shell variables).

Yves.

I'll make some notes from this for future use. Thanks everyone.


All times are GMT -5. The time now is 11:39 PM.