LinuxQuestions.org
Register a domain and help support LQ
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 02-13-2009, 02:13 AM   #1
curos
Member
 
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: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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

Code:
echo -e "xxx\n$(cat example_file.txt)" > example_file.txt
Edit: here's a solution using sed:
Code:
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.
 
  


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


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