38

In Linux if you type cat *, you will get something like this:

line1 from file1
line2 from file1
line1 from file2
line1 from file3
line2 from file3
line3 from file3

What I would like is to display a separator among files. Something like this:
line1 from file1
line2 from file1
XXXXXXXXXXXX
line1 from file2
XXXXXXXXXXXX
line1 from file3
line2 from file3
line3 from file3

Is that easily possible with a one-liner easy to type by heart?

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
Daniele
  • 661
  • 1
  • 7
  • 10

3 Answers3

54

If you're not too fussy about the appearance of the separator:

tail -n +1 *
19

cd /to/your/directory; for each in *; do cat $each; echo "XXXXXXXXXXX"; done

Janne Pikkarainen
  • 31,852
  • 4
  • 58
  • 81
13
awk 'FNR==1 && NR!=1 {print "XXXXXXXXXXXX"}{print}' *

Or

awk 'FNR==1 {print "XXXXXX", FILENAME, "XXXXXX"}{print}' *

Or

awk 'FNR==1 {print "XXXXXX File no. " ++count, "XXXXXX"}{print}' *

Using only Bash (no cat):

for file in *; do printf "$(<"$file")\nXXXXXXXXXXXX\n"; done

Edit:

In AWK 4:

awk 'BEGINFILE {print "XXXXXXXXXXXX"}{print}' *

You can use any separator such as the ones in the other examples in this answer. If you want the separator at the end of each file, change BEGINFILE to ENDFILE. It can still appear at the beginning of the script since it's a conditional (rather than implying execution order)..

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151