Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-13-2009, 02:13 AM   #1
Registered: Aug 2003
Distribution: Slackware
Posts: 72

Rep: Reputation: 15
prepending text dynamically to a file

I have a command line program (not mine) that checks the first line of a file for a certain string. For example, lets say it looks for three x's. If it doesn't find those three x's, it exits. Now I want to run this program on some files but they all don't have the three x's at the top. Is there a way to dynamically prepend that text to the file before it is passed to the program?

I have a way currently, but it doesn't do it "on the fly". I have to make temporary files first and then feed it into the command line program.

Let's say the command line program is called `check`

$ ./check example_file
Error: could not find three x's at the top of file
$ echo "xxx" > header
$ cat header example_file > example_file_prepended
$ ./check example_file_prepended
Success: three x's found

So I was wondering if there was some way to dynamically prepend to the file, before the check command line program reads the file.

Does that make sense?
Old 02-13-2009, 04:29 AM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
There are probably a hundred different ways to do what you want. You could certainly do it with sed or awk, for example, though I don't know the syntax for them offhand. But a quick and dirty way I've used is something like this:

echo -e "xxx\n$(cat example_file.txt)" > example_file.txt
Edit: here's a solution using sed:
sed -i 1i\ xxx example_file.txt
The -i flag in sed makes it modify the original file in place, 1i\ means insert text at line one, and is followed by the text you want to insert.

Last edited by David the H.; 02-13-2009 at 07:58 AM.


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
Linux Text File convert to Windows/Notepad Text File = Wrapped? backroger Linux - Software 4 01-18-2009 06:54 AM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 10:40 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM
Printing numbers from a text file dynamically mrobertson Programming 1 06-28-2005 09:19 AM
Compressing a file dynamically xanthium Programming 5 03-22-2002 06:06 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:45 AM.

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