LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2012, 10:36 AM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Rep: Reputation: Disabled
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
 
Old 10-24-2012, 11:02 AM   #2
jimsiak
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
Try this:

for file in ABC.txt DEF.txt GHI.txt; do
echo $file
cat $file
echo
done > summary.txt
 
Old 10-24-2012, 11:31 AM   #3
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
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.

Last edited by archShade; 10-24-2012 at 11:48 AM. Reason: Noticed error ""cat out.txt"" changed to ""out.txt""
 
1 members found this post helpful.
Old 10-24-2012, 04:31 PM   #4
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-24-2012, 06:51 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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/
 
1 members found this post helpful.
Old 10-24-2012, 07:35 PM   #6
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
Thanks Chris!

Tabby
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to write a batch file in linux?and what are batch(.bat) file benefits? karthilin Linux - Newbie 8 10-15-2012 02:10 PM
cat file over ssh and put it into log file sinister1 Programming 2 04-24-2009 12:54 AM
batch file for accessing a shared file in ubuntu samba pdelcast Linux - Networking 5 04-24-2008 12:21 AM
filesize limit on 'cat $file | sed > $file' drkstr Linux - Software 2 07-10-2006 02:47 AM
trying to redirect text to a file to cat at later point. says file doesn't exist. dr_zayus69 Programming 1 10-02-2005 08:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration