LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-24-2008, 11:03 AM   #1
davidstvz
Member
 
Registered: Jun 2008
Posts: 405

Rep: Reputation: 30
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).
 
Old 08-24-2008, 11:14 AM   #2
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS6
Posts: 267

Rep: Reputation: 58
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

Last edited by david1941; 08-24-2008 at 11:17 AM.
 
Old 08-24-2008, 11:25 AM   #3
davidstvz
Member
 
Registered: Jun 2008
Posts: 405

Original Poster
Rep: Reputation: 30
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)
 
Old 08-25-2008, 12:32 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
give an example, what do you have AND what do you want.
 
Old 08-25-2008, 03:18 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
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.
 
Old 08-25-2008, 06:10 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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.
 
Old 08-25-2008, 06:58 AM   #7
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Quote:
Originally Posted by davidstvz View Post
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.
 
Old 08-25-2008, 09:58 AM   #8
davidstvz
Member
 
Registered: Jun 2008
Posts: 405

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by theYinYeti View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting question (simple) Leon W. Malinofsky *BSD 8 07-31-2008 10:48 AM
Another simple scripting question Echo Kilo Linux - Newbie 10 05-14-2008 10:52 AM
simple scripting question jerf Linux - Newbie 4 09-19-2007 04:12 AM
Bash Scripting--Simple Question mooreted Linux - General 4 05-10-2004 02:44 PM
simple scripting question wedgeworth Programming 6 02-04-2004 11:30 AM


All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration