LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Sed – how do I save output to file with filename from content of another file? (http://www.linuxquestions.org/questions/programming-9/sed-%96-how-do-i-save-output-to-file-with-filename-from-content-of-another-file-865512/)

misarab 02-28-2011 11:00 AM

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

Stephen Morgan 02-28-2011 12:03 PM

sed 'stuff you put here' < unformatted > $(date -d now +%y-%m-%d)-formatted

Stephen Morgan 02-28-2011 12:10 PM

Also see if you've got pdftotext installed, if you want to save yourself the bother of copying and pasting the whole thing.

misarab 02-28-2011 01:15 PM

Thank you so much for your reply!

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
for example:
2011-02-25
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).

misarab 02-28-2011 02:12 PM

The solution.
 
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.


All times are GMT -5. The time now is 02:44 PM.