I've got 40 or so single line text files (notes taken from a an old Nokia phone)
I want to append these files to a single file.
cat Nokia*.txt >> notes
works fine, to give a space between lines, I use on the cli
cat Nokia*.txt >> notes | sed 'G' notes > notes
this works fine and produces the desired result.
when I make a a two line shell script to do this, using the same lines
#!/bin/bash cat Nokia*.txt >> notes sed 'G' notes > notes
the result is an empty file. I would be grateful for an explanation as to the reason for this, anyone help?
#!/bin/bash cat Nokia*.txt >> notes sed 'G' notes >> notes
produces a file with the the list and then the list double spaced, I can understand this.
#!/bin/bash cat Nokia*.txt >> notes1 sed 'G' notes1 > notes rm notes1
gives me the file 'notes' as wanted, I can understand this, it's the different results from the apparently same lines
on the cli and then the two line shell script which puzzles me.
Last edited by benhee (2014-01-14 20:19:28)
Your shell script is not the same as your command line. The | operator is not the same as putting the command on two lines. Simply use the same operator in your shell script with a single line.
The | operator is essentially what you did in your last example, which is to feed the output of the first command into the second command.
thanks Iann and Bronto (will ignore, but why?), more study required by me to understand the differences...good fun....
If you want to operate on one and the same file instead of reading from one file to another, you have to use the "-i" flag with sed. Like this:
sed -i 'G' notes