i try to concatenate two .txt in loops, here is the file
mn@LeNOVO-220414-A:/mnt/c/Users/LeNOVO/Alnus$ ls
Alnus1.txt Alnus2.txt Alnus3.txt Alnus4.txt Bobi1.txt Bobi2.txt Bobi3.txt Bobi4.txt`
I try to combine Alnus1 and Bobi1 into a new file named combination1.txt
I am new in bash and need guidance I here is my failed trial, please take a look.
mn@LeNOVO-220414-A:/mnt/c/Users/LeNOVO/Alnus$ ls
Alnus1.txt Alnus2.txt Alnus3.txt Alnus4.txt Bobi1.txt Bobi2.txt Bobi3.txt Bobi4.txt
mn@LeNOVO-220414-A:/mnt/c/Users/LeNOVO/Alnus$ for name in *1.txt
> do
> other = "${name/1/1}"
> cat "$name" "%other" > "$combination1"
> done
other: command not found
-bash: : No such file or directory
other: command not found
-bash: : No such file or directory
I try to combine Alnus1 and Bobi1 into a new file named combination1.txt
CodePudding user response:
This is probably what you're looking for:
for file in Alnus*.txt; do
suffix=${file#Alnus}
cat "$file" "Bobi$suffix" > "combination$suffix"
done
${file#Alnus}
removes the string Alnus
from the beginning of the value of the variable file
.
CodePudding user response:
If you know exactly how many files there are, you can do this:
for i in {1..4}; do
cat Alnus${i}.txt Bobi${i}.txt > combination${i}.txt
done
If you don't, you can use ls Alus*.txt | wc -l
to make it work for any number of files (assuming you are always starting the cound from 1), like this:
for i in $(seq 1 $(ls Alnus*.txt | wc -l)); do
cat Alnus${i}.txt Bobi${i}.txt > combination${i}.txt
done