LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 02-27-2012, 01:51 PM   #1
infinite_scale
Member
 
Registered: Feb 2011
Location: NCR, Uttar Pradesh, India
Posts: 78

Rep: Reputation: 1
How to add text to a file's first and last line without using vi


Hi,

We all know one can add text to a file by opening it in vi.

But is it also possible to add/append text to a file's, let's say, first and second line but without using vi i.e. directly through commands?
 
Old 02-27-2012, 02:29 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi infinite_scale,

This is the command I can think of for appending text to first and last line of a file. Ofcourse you can use script to make it better but this is the simplest way I could think to achieve what you have mentioned

Code:
cat testing > newfile && echo "This is first line" > testing && cat newfile >> testing && echo "This is the last line" >> testing
This is how it works. I have got a file name testing which contains say 100 lines. What this command will do it will first copy all those lines in a newfile. Then it will insert a text using echo command say "This is the first line". Then it will append the text which is there in newfile back to testing file. At the end using echo you will put the text "This is the last line"
 
1 members found this post helpful.
Old 02-28-2012, 12:23 AM   #3
infinite_scale
Member
 
Registered: Feb 2011
Location: NCR, Uttar Pradesh, India
Posts: 78

Original Poster
Rep: Reputation: 1
Thanks Terminator!

So simple commands can be used together to accomplish different tasks...

Thanks a lot!!
 
Old 03-02-2012, 08:10 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Using ed:
Code:
$ echo -e '0a\nfirst line\n.\n$a\nlast line\n.\nw' | ed file.txt
Using sed:
Code:
$ sed '1s/^/first line\n/; $alast line' file.txt
Add `-i' if you want to save changes in 'file.txt'.

Or simply
Code:
$ echo first line; cat file.txt; echo last line > file.txt.new

Last edited by firstfire; 03-02-2012 at 08:15 AM.
 
  


Reply



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
sed add data to the back of a particular line in a text file gengwei89 Linux - Newbie 1 11-06-2011 04:31 AM
PHP: Find a string in a text file and add something to that line aocferreira Programming 1 05-26-2011 01:52 PM
[SOLVED] can add a text line to text files? windstory Linux - Newbie 4 08-25-2009 06:40 AM
simple php script to add line/file to text file dnoy Programming 1 05-21-2008 05:08 PM
How to add a text to first line of a text file? oskeewow Linux - Newbie 6 04-23-2008 12:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 06:03 PM.

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
Open Source Consulting | Domain Registration