Skip to content
Advertisement

Centos shell script – Group files starting with the same string

I have a directory with many pdf files. Each file name starts with a numeric ID (5 chars fixed length), as follows:

11111-2014.pdf  
11111-2015.pdf  
22222-2015.pdf  
33333-2013.pdf  
33333-2014.pdf  
33333-2015.pdf  

I’m trying to figure out how to write a shell script in CentOS that reads files in such directory and list them depending on the ID. The shell script should return the following output (on 3 rows) as following:

11111-2014.pdf 11111-2015.pdf  
22222-2015.pdf  
33333-2013.pdf 33333-2014.pdf 33333-2015.pdf  

and assign to each row a variable name (ie: $rowstring).

I would like to group in a string all the files starting with the same ID, and attach them to the same email using mutt email client by command line. So, following the above example:

for each unique $rowstring the command would be:

mutt -s "subject" $rowstring -- abc@domain.com < body.txt  

where $rowstring (the attachments) should look like as follows
For the first email:

-a 11111-2014.pdf -a 11111-2015.pdf

For the second email:

-a 22222-2014.pdf  

For the third email:

-a 33333-2013.pdf -a 33333-2014.pdf -a 33333-2015.pdf

I tried to combine ls and awk commands but I’m too newbie to succeed. Thanks everybody.

Advertisement

Answer

The following command:

 ls  | awk -F '-' 'BEGIN{temp=""} temp&&$1!=temp{printf "n"} {printf "-a %s ",$0; temp = $1}'

outputs:

-a 11111-2014.pdf -a 11111-2015.pdf
-a 22222-2015.pdf
-a 33333-2013.pdf -a 33333-2014.pdf -a 33333-2015.pdf

You can then use each line one at a time and send mail.

Advertisement