0

I'm trying to use bash to read data from several one line text files, each in its own directory, and spit them out into one master file, each on a new line.

The data of my input file is structured like so ;

Store# online backoffice win7

I'm looping through each directory with find and cat the file contents to a variable. The problem is I can't seem to get each pass to put the data on a newline. Here is my code;

#!/bin/bash

#Find all sites
stores=$(find /home/sites*/ -maxdepth 0)


for store in $stores; do
  final=${final}$(cat ${store}\processors.txt)"\n"
done

printf "${final}" > ~/master.txt

What I get in the master file is

Store# online backoffice win7Store# online backoffice win7
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
3030tank
  • 99
  • 2
  • 10
  • It seems you only want `cat /home/sites*/processors.txt > ~/master.txt`. Are you reading your file `master.txt` on a windows machine? – gniourf_gniourf Feb 05 '15 at 13:39
  • Yes, reading it on a windows machine. – 3030tank Feb 05 '15 at 13:45
  • You might need to transform the unix newlines into dos newlines with the `unix2dos` utility, or to set your editor to handle unix newlines (if you have a decent editor, it should be doable). – gniourf_gniourf Feb 05 '15 at 13:47
  • You should quote `${final}` when adding new text to it or you lose all newlines.Also you are losing the newline from the shell command anyway. –  Feb 05 '15 at 14:26

1 Answers1

0

You can replace your script by this one-liner:

find /home/sites* -name "processors.txt" -exec cat {} + > ~/master.txt

As you will be reading in a windows machine, you may want to insert return carriage at the end of line:

 find /home/sites* -name "processors.txt" -exec cat {} + | sed -e 's/$/\r/' > ~/master.txt
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51