LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   batch cat with a file name (https://www.linuxquestions.org/questions/linux-newbie-8/batch-cat-with-a-file-name-4175433868/)

atjurhs 10-24-2012 10:36 AM

batch cat with a file name
 
Hi guys,

I need to run the cat command over several files, and have it keep adding onto a summary file, something like:

psuedo-code
Code:

cat ABC.txt DEF.txt > summary.sum
cat DEF.txt summary.sum > summary.sum
cat GHI.txt summary.sum > summary.sum
cat JKL.txt summary.sum > summary.sum
etc...

so I think I can do this part with a "for loop" if it was just this part.

but I also need to have the files' names inside the summary.sum file for each block of data that was cat'd in, something like this for the output summary.sum file format


Code:


ABC.txt
calc1 = 123
calc2 = 456
calc3 = 789

DEF.txt
calc1 = 001
calc2 = 010
calc3 = 100

GHI.txt
calc1 = -.015
calc2 = 462
calc3 = 9078.75

JKL.txt
calc1 = 987
calc2 = 654
calc3 = 321

can someone here please help me,

Tabitha

jimsiak 10-24-2012 11:02 AM

Try this:

for file in ABC.txt DEF.txt GHI.txt; do
echo $file
cat $file
echo
done > summary.txt

archShade 10-24-2012 11:31 AM

You need to use the echo command to print the file name I threw together a quick script that did what you asking

Code:

#!/bin/bash

output_file="out.txt"

for File in "$@"
do
        echo $File >> $output_file
        cat  $File >> $output_file
        echo "" >> $output_file
done

Note the use of the varible @ this is all the passed arguments wich is then looped though. The last echo adds a new line

I tried it on 4 files named exfile_1, exfile_2 etc.. Which took the form:
Code:

File1_Line1
File1_Line2
File1_Line3
File1_Line4

After calling the script like this
Code:

./cat_files_with_names.sh exfile_{1..4}.txt
A new file "out.txt " was created wich contained:
Code:

exfile_1.txt
File1_Line1
File1_Line2
File1_Line3
File1_Line4

exfile_2.txt
File2_Line1
File2_Line2
File2_Line3
File2_Line4

exfile_3.txt
File3_Line1
File3_Line2
File3_Line3
File3_Line4

exfile_4.txt
File4_Line1
File4_Line2
File4_Line3
File4_Line4

Also note that if the script is run twice there the new results are appended to the file in totality.
HTH - let me know if this is the solution you where looking for.

atjurhs 10-24-2012 04:31 PM

I used the command:

Code:

sh ./cat_files_with_names.sh exfile*.txt
and that seemed to work, giving exactly the outcome I need, thanks sooooooo much :)

I have never seen the bash "$@" what is that doing?

and what's the difference between > and >>

thanks again,

Tabitha

chrism01 10-24-2012 06:51 PM

If you use the shebang line at the top of a shell script thus
Code:

#!/bin/sh
then there's no need to say 'sh ./my.sh ..', just use './my.sh ..'

The input params to a shell script can be expressed as an array ($@) or as a single string ($*).

> = overwrite/start a new file
>> = append to target

See these links for ref
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

atjurhs 10-24-2012 07:35 PM

Thanks Chris!

Tabby


All times are GMT -5. The time now is 05:40 AM.