2

I have on a folder multiple txt file containing one or several lines. Each file name is an email address containing different email(s) address(es) inside.

For example, I have 3 files on my folder :

  • distribution-list1@example.com.txt
  • distribution-list2@example.com.txt
  • distribution-list3@example.com.txt

Content of each files:

cat distribution-list1@example.com.txt
john@example.com
aurel@example.com

cat distribution-list2@example.com.txt
doe@example.com

cat distribution-list3@example.com.txt
jack@example.com
gilbert@example.com
jane@example.com

I would like to build only one file containing those data:

distribution-list1@example.com;john@example.com
distribution-list1@example.com;aurel@example.com
distribution-list2@example.com;doe@example.com
distribution-list3@example.com;jack@example.com
distribution-list3@example.com;gilbert@example.com
distribution-list3@example.com;jane@example.com
Serenity
  • 35,289
  • 20
  • 120
  • 115
zakiz
  • 43
  • 5
  • Possible duplicate of [Looping through the content of a file in Bash?](http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash) – grimsock Oct 20 '15 at 10:56

2 Answers2

3

lists_merge.sh

#!/usr/bin/env bash

shopt -s nullglob;
for fname in *.txt;
do
  while read line; 
  do
    printf "%s;%s\n" "$fname" "$line";
  done <"$fname";
done;

output

$ ./lists_merge.sh 
distribution-list1@example.com.txt;john@example.com
distribution-list1@example.com.txt;aurel@example.com
distribution-list2@example.com.txt;doe@example.com
distribution-list3@example.com.txt;jack@example.com
distribution-list3@example.com.txt;gilbert@example.com
distribution-list3@example.com.txt;jane@example.com

note: script assumed to be in same directory as distribution list text files. assumed no other text files are in this directory


reference

nullglob info

amdixon
  • 3,814
  • 8
  • 25
  • 34
  • Why did you use the line ``shopt -s nullglob;``? – gpupo Oct 20 '15 at 11:11
  • this is to handle the case that there are no *.txt matches for the glob. in this case bash would throw an error without the nullglob option. see also [this question](http://stackoverflow.com/questions/2937407/test-whether-a-glob-has-any-matches-in-bash) – amdixon Oct 20 '15 at 11:21
  • You can safely remove all the semicolons from your script - they are only needed when you put two separate statements on one line. – Mark Setchell Oct 20 '15 at 13:25
  • yes, they are optional. my style preference is to include them ( feels closer to other languages i use ). from an error-rate perspective though always having semicolons after while loop statements helps even if i dont have a newline before the do statement – amdixon Oct 20 '15 at 13:34
1

You can use sed:

for emailfile in *.txt; do
    email=${emailfile%.txt}
    sed "s:^:$email;:" "$emailfile"
done

This will fail if an email ID has a colon (:), but I doubt you'd have such an example.

svsd
  • 1,831
  • 9
  • 14