Issue
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
Solution
This is probably what you're looking for:
for file in Alnus*.txt; do
suffix=${file#Alnus}
cat "$file" "Bobi$suffix" > "combination$suffix"
done
The value of ${file#Alnus}
is the value of the variable file
with the string Alnus
removed from the beginning.
Answered By - M. Nejat Aydin Answer Checked By - Candace Johnson (WPSolving Volunteer)