Reputation: 3022
I am trying to use bash
to combine the contents of two text files from different folders in the same directory. The below does execute but produces the current output below. Hopefully it is close but I am not sure why it is not producing the desired output. Thank you :).
bash
topdir=/home/cmccabe/Desktop/s5.2
dir1=/home/cmccabe/Desktop/s5.2/Folder1
dir2=/home/cmccabe/Desktop/s5.2/Folder2
for f in $dir1/*.txt
do
$(basename "$f")
outf=$topdir/all.txt
cp $f $outf
sed -e '1 d' $dir2/*.txt >> $outf
done
Description of directory layout:
toplevel directory `/home/cmccabe/Desktop/s5.2`
|
Folder1 Folder2 `/home/cmccabe/Desktop/s5.2/Folder1` or `/home/cmccabe/Desktop/s5.2/Folder2`
| |
.txt .txt `$dir1.txt` or `$dir.txt`
Contents of text file from dir1
xxx_000 00-0000_La-Fi
xxx_001 00-0001_L-F
xxx_002 00-0002_xx-xxx
R_2017_SomeName
xx_0000 xxx
xx_0001 xxx1
xx_0002 xxx2
xx_0003 xxx3
R_2017_SomeName
Contents from text file at dir2
xxx_006 00-0000_La-Fi
xxx_007 00-0001_L-F
xxx_008 00-0002_xx-xxx
R_2017_SomeName
current output
file1.txt command not found in the terminal but `all.txt` is created and looks like:
xxx_000 00-0000_La-Fi
xxx_001 00-0001_L-F
xxx_002 00-0002_xx-xxx
R_2017_SomeName
xx_0000 xxx
xx_0001 xxx1
xx_0002 xxx2
xx_0003 xxx3
R_2017_SomeName --- not carrying over the newline ---
xxx_007 00-0001_L-F
xxx_008 00-0002_xx-xxx
R_2017_SomeName
desired result
xxx_000 00-0000_La-Fi
xxx_001 00-0001_L-F
xxx_002 00-0002_xx-xxx
R_2017_SomeName
xx_0000 xxx
xx_0001 xxx1
xx_0002 xxx2
xx_0003 xxx3
R_2017_SomeName
xxx_006 00-0000_La-Fi
xxx_007 00-0001_L-F
xxx_008 00-0002_xx-xxx
R_2017_SomeName
Upvotes: 0
Views: 51
Reputation: 20032
You do not need the for-loop.
topdir=/home/cmccabe/Desktop/s5.2
dir1=/home/cmccabe/Desktop/s5.2/Folder1
dir2=/home/cmccabe/Desktop/s5.2/Folder2
outf=$topdir/all.txt
cp $dir1/*.txt $outf
# The question is not complete clear to me,
# the next echo can be deleted when you do not want an empty line
echo >> $outf
sed -e '1 d' $dir2/*.txt >> $outf
or
cat $dir1/*.txt <(echo) <(sed -e '1 d' $dir2/*.txt) > $outf
Upvotes: 1
Reputation: 85895
You can perhaps make things simple by running a loop like below using brace expansion from the /home/cmccabe/Desktop/s5.2/
as
outf="/home/cmccabe/Desktop/s5.2/final.txt"
for f in ./Folder{1,2}/*.txt; do
printf "%s\n" "$(< $f )" >> "$outf"
done
Upvotes: 2