Bash question

Bill Heagy wheagy1-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org
Wed Dec 21 03:19:05 UTC 2011


jim wrote:
>
>>
>>> Hi,
>>> I have a bash or scripting question. I have a few hundred files in a
>>> directory, They have file names formatted similar to below:
>>> 6230718_1puJ0P8Z_file1.pdf
>>> 6230718_b9nP4YtD_file2.pdf
>>> 6230718_O1F0Qj0p_file3.pdf
>>> 6233064_c9yOocTq_file4.pdf
>>> 6233064_ON8OdanT_file5.pdf
>>> 6233064_X64AbjZZ_file6.pdf
>>>
>>> I want to take all the files that have the same 7 digit numbers at the
>>> front end of the file names and join the PDF files. There are not always
>>> 3 files associated with the same 7 digit number. Sometimes there are
>>> more or less than 3 pdf files to join. I figured I can use pdftk to join
>>> the files but trying to figure out how to easily select and sort a
>>> random number of PDFs has me a bit stumped. Any takers?
>>> Thanks,
>>> Jim
>> Jim,
>>
>>      For any sort of weird, complicated text processing, I strongly recommend Perl.
>>
>>      You can capture the directory list by going ls 6233064*.  You can feed this to the next command.
>
> Thanks. I would prefer not to have to do an ls  for each 7 digit number.
> There are at least 100 of these unique 7 digit numbers in the directory.
> I was thinking I may have to use the linux " uniq " command in here to
> somehow first get all the unique numbers??
>
> Jim
>>
>
>
> --
> The Toronto Linux Users Group.      Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
>
I think something like

for i in `ls [0-9]* | cut -b1-7|uniq`;do foobar ${i}*pdf -o new${i}.pdf;done

will work.  This assumes that all files starting with a digit are 
eligible: adjust as required.  I don't know the syntax for your pdftk 
command: adjust this as required also.  Beware using ">" for redirecting 
output though: that requires careful use of quotes.

(This is not gospel: I'm not a bash expert.)


-- 
Bill Heagy
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list