justaguy
justaguy

Reputation: 3022

combine files same prefix text files into one

I am trying to use bash to merge/combine all text files in a directory with the same prefix into one text file. Thank you :).

directory

111.txt
aaa
aaa
222_1.txt
bbb
222_2.txt
ccc
ccc
333_1.txt
aaa
333_2.txt
ccc
ccc
333_3.txt
bbb

desired

111.txt
aaa
aaa
222.txt
bbb
ccc
ccc
333.txt
aaa
ccc
ccc
bbb

bash

for file in `ls`|cut -d"_" -f1 ; do
  cat ${file}_* > ${file}
done

Upvotes: 0

Views: 471

Answers (2)

chepner
chepner

Reputation: 530960

This is a good use of an associative array as a set. Iterate over the file names, trimming the trailing _* from each name before adding it to the associative array. Then you can iterate over the array's keys, treating each one as a filename prefix.

# IMPORTANT: Assumes there are no suffix-less file names that contain a _
declare -A prefixes
for f in *; do
    prefixes[${f%_*}]=
done

for f in "${!prefixes[@]}"; do
    [ -f "$f".txt ] && continue  # 111.txt doesn't need anything done
    cat "$f"_* > "$f".txt
done

Upvotes: 6

francois P
francois P

Reputation: 392

build a test environment just as you did

mkdir -p tmp/test
cd !$
touch {111,222,333}.{txt,_2.txt,_3.txt}
cat > 111.txt
aaa
aaa

and so on

then you know how to increment filnames :

for i in $( seq 1 3 ) ; do echo $i*  ; done                                                                                                                
111._2.txt 111._3.txt 111.txt
222._2.txt 222._3.txt 222.txt
333._2.txt 333._3.txt 333.txt

so you make your resulting files and here is the answer of mechanism to your needs :

for i in $( seq 1 9 ) ; do  cat $i* >> new.$i.txt  ; done

and finaly

ls -l new.[1-3]*
-rw-r--r-- 1 francois francois 34 Aug  4 14:04 new.1.txt
-rw-r--r-- 1 francois francois 34 Aug  4 14:04 new.2.txt
-rw-r--r-- 1 francois francois 34 Aug  4 14:04 new.3.txt

all 3* contents are in new.".txt for example here. you only have to set the desired file destination to add in the content & if needed but not set in initial question a sorting of datas by alphabetic order or numerical... etc

Upvotes: 1

Related Questions