Skip to content
Home ยป Blog ยป Bash Script: Merge lists

Bash Script: Merge lists

I see you copy&pasting your scraped lists together ๐Ÿ‘€

Just joking. I’m sure you figured that out.

I want to give you a little script for doing that anyway. Also to help the people who are really just getting started into this.

Again, a great example on how some scripting skills can save you time and money. No searching on the internet for a tool, no interruption in your building process. Imagine you found a tool that merges three lists, but now you want to merge five and that tools can only do three…

Anyway, a word about the script. Nested loops are never good. But this is only for one time use. We will most likely never work on the script again. So please forgive me the poor coding style.

Here’s the little script (save it as merge.sh for example):

#!/bin/bash

for LINE1 in `cat $1`
do
    for LINE2 in `cat $2`
    do
	for LINE3 in `cat $3`
	do
		echo $LINE1 $LINE2 $LINE3
	done
    done
done


Run it like this:

./merge.sh list1.txt list2.txt list3.txt >output.txt


Again, don’t forget to make it executable first:

chmod +x merge.sh


Have fun.

Leave a Reply

Your email address will not be published. Required fields are marked *