0

I need to write a "find" exec command to awk print formatted results to some existing text files. In some odd scenario, I would get lines appended on the previous last line (no CR after LF?), not on a new line. How could I prevent this from happening? is there any command I could fix the text files before the append? dos2unix?

  • 1
    https://unix.stackexchange.com/a/31955/70524 – muru Oct 08 '20 at 04:54
  • 2
    Have you checked (for example with hexdump -C filename) what the problem really is? Linux just uses LF as a line separator, so it can't be "no CR after LF". Editing your question with the commands you use, an example of a file where it goes wrong and what results you get would help. – dirkt Oct 08 '20 at 04:54
  • @dirkt if a unix file is not terminated by a newline, it's technically not a text file, and many Unix tools working on text files are allowed to "misbehave", and will misbehave (the most notorious being the read shell builtin: printf hello | read q || echo not read, why?. An easy way for me to create such broken non-text files is by copy-pasting via the GUI ;-) –  Oct 08 '20 at 05:25

2 Answers2

1

Use:

awk 1 file1 file2 ... > outfile

instead of cat file1 file2 ... > outfile The only problem is that unlike cat, awk will immediately exit with an error if it cannot open and read any of file1, file2, etc, instead of continuing with the rest of the files. If that's a problem, on most modern systems you can use grep '' as already suggested here + the -h option to omit the filenames (though beware that this is exploiting non-standard features of grep):

grep -h '' file1 file2 ... > outfile
1

there's an interesting hack where you can format bash command with awk and push it to bash like so:

awk '{ print "find " $1 " -exec ls {} \;" }' ./source_file | bash

this will give you instant execution of commands such as:

find /var -exec ls {} \;
find /home/user1 -exec ls {} \;
# and so on depending on the contents of the ./source_file

hope this helps :)

Roman Spiak
  • 299
  • 1
  • 6