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