[SOLVED] Sed – how do I save output to file with filename from content of another file?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Sed – how do I save output to file with filename from content of another file?
My employer issues pdf files with everyone’s work schedules. I copy the content and save it as plain text in a file called “unformatted” (hope to be able to automate this step someday). I’m working on a SED script that reduces “unformatted” to only display what I want to see and saves the result in a file I’ve named “formatted”. After that I have to manually copy “formatted” and save it with that days date as a filename e.g. “2011-02-25” or whatever day is scheduled in the pdf, for use on a mobile device (Nokia N900). I noticed that the date occurs on certain lines in the file so I added a line like:
sed -n 's/^Date: \(201[1-9]\)\/\([0-1][0-9]\)\/\([0-3][0-9]\).*/\1-\2-\3/p' < unformatted >theDate
That creates a file “theDate” with the date in it that I wish to use as the filename for this particular instance. So I would like to skip the file “formatted” all together and have the sed- script write to a new file using the content of “theDate” as a filename, but how do I make that happen?
And of course it would be more elegant if I could skip the intermediate “theDate” file as well….
I see that I must have been unclear about the date. It’s always in the future and you never know in how many days. The date of interest is however written, in plain text, inside the file “unformatted” amongst a lot of other text. I have managed to extract the date and write it in a file called “theDate”. The content of the file “theDate” is precisely the date of interest as a string
nothing more and nothing less.
My basic problem is that I wish to use the content of a file (that string inside “theDate”) as a filename for another file (containing the output from a sed command).
Ok, I stumbled upon something that gets the job done, even if this solution isn’t quite what I expected:
sed -n 's/.*/&/p' < formatted >$(cat theDate)
Quite simple really. Hope this helps someone else.