0

I want to make a report which is the concatenation of several .txt files. I wrote this bash code (~/scripts/report.sh)

echo "Display content of files in /etc/ directory"
for file in $(find ~/Research/reports/August/ -name '*.txt' -type f) ;
do
    echo "############START OF FILE#########"
    echo ${file}
    echo " "
    cat ${file}
    echo "##########END OF FILE########"
    echo " "
done

but I get the error:

'home/vdonchev/scripts/report.sh: line 2: syntax error near unexpected token `
'home/vdonchev/scripts/report.sh: line 2: `for file in $(find ~/Research/reports/August/ -name '*.txt' -type f) ;

I don't understand what is wrong: when I call only

find ~/Research/reports/August/ -name '*.txt' -type f

it works and indeed lists my .txt files.

Thanks in advance.

Veliko
  • 747
  • 1
  • 9
  • 25
  • I actually took the code from here: http://www.unix.com/shell-programming-and-scripting/204319-recursively-cat-files-directory-filename-printed-first.html – Veliko Aug 31 '17 at 12:29
  • 2
    I tried running the code on Ubuntu 14 in Bash shell, changed `~/Research/reports/August/` to `/home/campovski/` and it worked... – campovski Aug 31 '17 at 12:31
  • 1
    I tried running the code on macOS, changed the directory to `~/Desktop` and it worked. From the log, it seems that there is a `\`` in your script but there is seems none in the script you posted – Papershine Aug 31 '17 at 12:40
  • @campovski thanks you can see my answer. – Veliko Aug 31 '17 at 12:42
  • Skip the loop and use `find ~/Research/reports/August/ -name '*.txt' -type f -printf '############START OF FILE#########\n%p\n\n' -exec cat {} \; -printf '##########END OF FILE########\n'` – Tom Fenech Aug 31 '17 at 12:44

3 Answers3

1

I resolved the issue. It seems that because I am opening my script through Windows some encoding gets disturbed.

After executing

dos2unix ~/scripts/report.sh 

it worked like a charm :)

I hope I explained correctly.

Veliko
  • 747
  • 1
  • 9
  • 25
0

Did you try

for filename in ~/Research/reports/August/*.txt; do
Frankx Ix
  • 15
  • 3
  • That is different because it only goes through `.txt` files directly in `~/Research/reports/August/`, not in subdirectories... – campovski Aug 31 '17 at 12:37
0

For me,

#/bin/sh

for file in $(find /path/to/repertory -name "*.txt" -type f)
do
   echo "############START OF FILE#########"
   cat $file
   echo "##########END OF FILE########"
done

did the trick. Then you should just launch like this :

 ./script.sh > concat.txt 
cydef
  • 417
  • 3
  • 10