merging text files into one

Hi good day
Is there a shell script I can use to join multiple text files in a folder and put the name(excluding the extension) of the text file before it's contents and put EOF at the end of each portion.
so for example, say I have

file1.dat, file2.dat, file3.dat

i'll get

file1
{text from file1}
EOF
{blank line}
file2
{text from file2}
EOF
{blank line}
file3
{text from file3}
EOF

thanks much.

Try:

awk 'FNR==1{if(NR>1)print x;print FILENAME}1' file[123].dat

Hi,

Test this solution. The last character is '*', witch selects all files in the directory where you run the script.

$ perl -e 'while (@ARGV) { ($param = $ARGV[0]) =~ s/(.*)\..*/\1/ ; print "$param", "\n", (qx/cat $ARGV[0]/), "EOF\n\n" ; shift @ARGV }' *

Regards,
Birei

1 Like

Sorry but I'm new to this
This doesn�t work. I have a format.sh file in the folder that I run from the shell using �sh format.sh�.
I was hoping to see something like this
Step 1 (or line one of script): ls *.dat | while read file; do awk �magic code �to and insert filename, without extension, into the text plus EOF at end� here� $file > file_formatted ; done
Step 2 (or line two of script): code to join all the file_formatted files that were created in step 1 with a blank line between each.

thanks

---------- Post updated at 07:19 AM ---------- Previous update was at 07:14 AM ----------

now testing code from birei..1 sec

---------- Post updated at 07:20 AM ---------- Previous update was at 07:19 AM ----------

now testing code from birei..1 sec

You can use awk to do it all:

awk 'FNR==1{if(NR>1)print x;f=FILENAME;sub(/\.[^.]+$/,x,f);print f}1' file[123].dat > outfile

or use shell:

first=true
for f in file[123].dat
do
  if ! $first; then
    echo
  fi
  first=false
  echo "${f%.*}"
  while IFS= read -r line
  do
    printf "%s\n" "$line"
  done < "$f"
done > outfile

But I would not repeatedly call awk within a loop....

the perl script from birei works brilliantly....thanks

---------- Post updated at 10:20 AM ---------- Previous update was at 10:15 AM ----------

scrutinizer...thanks also but I am having difficulty doing it with your method...
Although your stuff looks easier to understand....birei stuff works on the first go, so i used it.

Hi you're welcome. Good that you found a solution. For the interest of this thread are you on Solaris? In that case you will find that most awk solutions on this forum will only work with /usr/xpg4/bin/awk instead of awk ( the same goes for sed, grep, and even sh )

I'm on redhat linux