How to read file names from a txt file in cmd prompt(script) and then combine a subset of the files into a PDF file using the cmd line in windows - shell

I have a text file in the following format:
2014-05-13 03:35 PM 48,841 SUR2-**C01**-00-000-PCE-1001-002.pdf
2014-05-13 03:36 PM 43,599 SUR2-**C01**-00-000-PCE-1002-001.pdf
2014-05-13 03:35 PM 51,900 SUR2-**C02**-00-000-PCE-1000-001.pdf
2014-05-13 03:35 PM 53,622 SUR2-**C02**-00-000-PCE-1000-002.pdf
2014-05-13 03:35 PM 52,145 SUR2-**C02**-00-000-PCE-1000-003.pdf
2014-05-13 03:35 PM 50,426 SUR2-**C02**-00-000-PCE-1000-004.pdf
I need to parse this file, and pull out files that match C01 or C02, and send these files to a combined PDF file, one for C01 and one for C02.
How can i parse the file, and string match the file names on C01 or C02?
Then, how can i take the above parse result, and using the file names found, combine them into a PDF from the command line or in script?

merge-C01.bat
1.use awk to parse the file to array
set LIST=($(awk '/C01/{print $5}' file.txt))
2.use pdf merge tool (pdftk) to merge files in array
for %%i in %LIST% do pdftk out.pdf %%i cat output out.pdf

Related

How to disable or delete the new line (CRLF) that is added after a command?

when I try "dir" I get:
02/13/2020 03:29 PM <DIR> Saved Games
02/13/2020 03:29 PM <DIR> Searches
02/16/2020 12:57 PM <DIR> Videos
1 File(s) 0 bytes
16 Dir(s) 21,887,438,848 bytes free
<<<<<<<this new line is what I want to delete or disable
I tried commands like
findstr "." test.txt > output.txt
but they all add that new line at the end.
Thank you
EDIT:
I export "dir /B" to a file > output.txt
I need this because another program uses the list, and the last new line crashes it.
The program that loops through that list is hardcoded, so I need to provide it a list without the blank line.

Parse line for specific date format?

Writing a script using bash. I am trying to look through lines in a file for a specific date format:
date +"%a %b %d %T %Z %Y"
For example, if the line were
/foo/bar/foobar this 12 is 411 arbitrary stuff in the line Wed Jun 10 10:10:10 PST 2017
I would want to obtain Wed Jun 10 10:10:10 PST 2017.
Any way to search for specific date formats?
I'm not sure whether you'll agree with this approach. But if this is for some quick, non-recurring work, I won't look for a perfect solution that can handle all the scenarios.
To start with, you can use the following too generic pattern to match the part you want.
cat file | sed -n 's/.*\(... ... .. ..:..:.. ... ....\).*/\1/p'
Then you can enhance this further restricting the matches as you need.
E.g.
cat file | sed -n 's/.*\([a-Z]\{3\} [a-Z]\{3\} [0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] [A-Z]\{3\} [0-9]\{4\}\).*/\1/p'
Note that this still is not perfect and can match invalid contents. If you find it still not good enough, you can further fine tune the pattern to the point you want.

Convert .CSV file to .txt file using bash

I have a CSV with lots of lines delimited by comma. I have to convert it to text.
66012523,39,Feb 02 2015 05:19AM,
66012523,39,Feb 02 2015 09:53AM,
66012523,39,Feb 02 2015 01:38PM,
I used command cp source.csv destination.csv and also cat source.csv > destination.txt but it does output in the same format witch each line coming in new one. It just gets appended together. It outputs like
66012523,39,Feb 02 2015 05:19AM,66012523,39,Feb 02 2015 09:53AM,66012523,39,Feb 02 2015 01:38PM
How do I make them to output each line in newline. Please help.
I hypothesise that the first block in your question is what you WANT, and what you actually HAVE is
66012523,39,Feb 02 2015 05:19AM,66012523,39,Feb 02 2015 09:53AM,66012523,39,Feb 02 2015 01:38PM
So what you want to do, I hypothesise, is split this up into separate lines.
Am I right?
This is a bit rough-and-ready but it works. Relies on each group ending "M," and there being no other "M,"s in the text - which there aren't but I wouldn't call it robust.
sed s/M,/'\n'/g source.csv

Use cat to combine mp3 files based on filename

I have a large number of downloaded radio programs that consist of 4 mp3 files each. The files are named like so:
Show Name - Nov 28 2011 - Hour 1.mp3
Show Name - Nov 28 2011 - Hour 2.mp3
Show Name - Nov 28 2011 - Hour 3.mp3
Show Name - Nov 28 2011 - Hour 4.mp3
Show Name - Nov 29 2011 - Hour 1.mp3
Show Name - Nov 29 2011 - Hour 2.mp3
Show Name - Nov 29 2011 - Hour 3.mp3
Show Name - Nov 29 2011 - Hour 4.mp3
Show Name - Nov 30 2011 - Hour 1.mp3 and so on...
I have used the cat command to join the files with great success by moving the four files of the same date into a folder and using the wildcard:
cat *.mp3 > example.mp3
The files are all the same bitrate, sampling rate, etc.
What I would like to do is run a script that will look at the file name and combine hours 1-4 of each date and name the file accordingly. Just the show name, the date and drop the 'Hour 1'.
I looked around and found a number of scripts that can be used to move files around based on their names but I'm not adept enough at bash scripting to be able to understand the methods used and adapt them to my needs.
I'm using Ubuntu 14.04.
Many thanks in advance
You can use a bash for loop to find each distinct date name and then construct the expected mp3 names from that.
Because your files have spaces in their names and my solution uses globbing, you'll also have to edit your Internal Field Separator to ignore spaces for the duration of the script.
SAVEIFS=$IFS
IFS=$'\n\b'
for mdy in `/bin/ls *mp3 | cut -d' ' -f'4,5,6' | sort -u`; do
cat *${mdy}*.mp3 > "showName_${mdy}_full.mp3"
done
IFS=$SAVEIFS
This won't alert you if some hours are missing for some particular date. It'll just join together whatever's there for that date.
Note: The comment pointing out that cat probably won't work for these files is spot on. The resulting file will probably be corrupted. You probably want to use something like mencoder or ffmpeg instead. (Check out this thread.)

Is it possible to read a file's modification date with Ruby?

Is it possible to read a file's modification date with Ruby? I have successfully opened a text file and captured the contents of the file with
File.open("test.txt", "r").each do |line|"
but it would be very useful to read the modification date of the file.
Use mtime:
File.mtime("testfile")
=> 2014-04-13 16:00:23 -0300
"Returns the modification time for the named file as a Time object."
File.mtime("testfile") #=> Tue Apr 08 12:58:04 CDT 2003
Check this.

Resources