LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-28-2011, 12:00 PM   #1
misarab
LQ Newbie
 
Registered: Feb 2006
Distribution: Debian
Posts: 5

Rep: Reputation: 0
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….
 
Old 02-28-2011, 01:03 PM   #2
Stephen Morgan
Member
 
Registered: Feb 2011
Location: England
Distribution: Slackware
Posts: 154

Rep: Reputation: 15
sed 'stuff you put here' < unformatted > $(date -d now +%y-%m-%d)-formatted
 
Old 02-28-2011, 01:10 PM   #3
Stephen Morgan
Member
 
Registered: Feb 2011
Location: England
Distribution: Slackware
Posts: 154

Rep: Reputation: 15
Also see if you've got pdftotext installed, if you want to save yourself the bother of copying and pasting the whole thing.
 
Old 02-28-2011, 02:15 PM   #4
misarab
LQ Newbie
 
Registered: Feb 2006
Distribution: Debian
Posts: 5

Original Poster
Rep: Reputation: 0
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).
 
Old 02-28-2011, 03:12 PM   #5
misarab
LQ Newbie
 
Registered: Feb 2006
Distribution: Debian
Posts: 5

Original Poster
Rep: Reputation: 0
Talking 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.
 
  


Reply

Tags
filename, scripting, sed, write


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
Script to find tar archives, read tar file contents, output content to an index file. bluesword1969 Linux - General 4 02-07-2011 01:15 PM
How to make alterations to a file using sed , saving changes and retaining filename ? uncle-c Linux - General 2 07-02-2009 11:46 AM
converting file encoding (batch files) with same output filename yuubouna Linux - Newbie 1 01-14-2007 09:32 PM
using commands to output path, filename and info to a txt file bob_man_uk Linux - General 3 05-11-2006 03:31 PM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 01:54 AM


All times are GMT -5. The time now is 09:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration