LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-22-2008, 10:45 AM   #1
fsloke
LQ Newbie
 
Registered: Feb 2008
Posts: 9

Rep: Reputation: 0
How to add a word at the front of a file?


For example I have a file called testFile.txt content STRING as below

Quote:
test1
test2
We normally append a STRING in to end of the file using

# echo.END >> testFile.txt

The result is

Quote:
test1
test2
END
I curious how can I append at STRING at the front of the file which show as below

Quote:
START
test1
test2
END
Thank you

-fsloke
 
Old 12-22-2008, 11:00 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A method using sed:
Code:
sed -i '1i START' testfile
 
Old 12-22-2008, 04:01 PM   #3
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
A method using echo and cat
Code:
echo "START" >testfile2
cat testfile >>testfile2
Of course, using sed is more elegant.
 
Old 12-22-2008, 08:02 PM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Using Perl:
Code:
perl -i.bak -ple 'if ($. == 1) { $_ = "START\n$_"; }' file1
That will make a backup of the original as file1.bak, just in case you make a typo.
 
  


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
How could i add to the beginning of line word? DoME69 Programming 6 09-17-2008 02:55 AM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 09:46 PM
putting word in front of each line in a file stryka Slackware 3 12-06-2005 02:32 PM
add word to each line of the file? ziggie216 Linux - General 4 04-25-2005 04:19 AM
Add a notice of the top 20 commands to check your unix system on the front page t3gah LQ Suggestions & Feedback 1 03-21-2005 08:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 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