include file name to extracted files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting include file name to extracted files
# 1  
Old 04-27-2011
include file name to extracted files

I've written the script below to merge only .txt files that exist in one directory into one huge .txt file and ignore other files with other extensions.
now the result is one huge .txt file with all the contents of other .txt files

how can i add a File Name as a comment before each file?

Code:
//FileName

Code:
system='/path/to/my/directory'
cat `find ${system}  -name '*.txt'` > outputFileDirectoryName.txt

# 2  
Old 04-27-2011
Here's a ksh script that would do the trick.

Code:
#!/bin/ksh
 
for txtfile in $(ls $system/*.txt)
do
    echo "# Filename: $txtfile"
    cat $txtfile >> outputFileDirectoryName.txt
done

# 3  
Old 04-27-2011
i think i should specify the path in the system because i want to merge each directory with all .txt files in it in a separate file.
meaning
Directory A: has many files, i want to merge all .txt files in this directory only
Directory B: same thing merge all .txt file exist in this directory only.
and so forth.

when i executed the script u posted I got this error:
ls: cannot access /path/to/my/directories/*.txt: No such file or directory

Code:
#!/bin/ksh
system='/path/to/my/directories/DirectoryA'
for txtfile in $(ls $system/*.txt)
do
 echo " #FileName : $txtfile"
 cat $txtfile >> outputFileA.txt
done


When i removed system='/path/to/my/directory/DirectoryA'
i got this error
ls: cannot access /*.txt: No such file or directory
# 4  
Old 04-27-2011
From the errors it gave you, it sounds like it's having trouble finding either the files or directories. I would verify that your path is correct. You could add a test for the directory in the script that could tell you this:

Code:
 
#!/bin/ksh

system='/path/to/my/directories/DirectoryA'
 
if [ -d $system ]  # If the directory exists..
then
    for txtfile in $(ls $system/*.txt)
    do
        echo " #FileName : $txtfile"
        cat $txtfile >> outputFileA.txt
    done
else
    echo "Sorry, but that directory doesn't exist."
fi


As for wanting to repeat this on more directories, you could wrap a for loop around the above code like this:
Code:
for mydir in dirA dirB dirC
do
    echo "Now processing $mydir ..."
    # ... code from above ...
done

This is where I would define a function and pass the directory you want to process to the function as an argument. I hope that helps you.
# 5  
Old 04-27-2011
How about?
Code:
find "$system" -name '*.txt' -exec sh -c "echo '# {}';cat {}" \;

# 6  
Old 04-27-2011
Thank you for your response.
I think i didnt explain what i need clearly,
I have 145 directories each one has many files,
for example one of the directories is
ClassA that has grade.txt subjects.txt courses.txt description.xml
i want to have one file called ClassA.txt that contanis all the contents of grade.txt , subject.txt , courses.txt
and each part in ClassA.txt should have comment '//' and the file name
i.e
ClassA.txt
would be like
//grade.txt
[content of grade .txt]
// subject.txt
[content of subject.txt]
and so forth

what i've managed to do so far is
Code:
#!/bin/sh
system='/home/path/to/first/directory'
for txtfile in `find ${system} | grep "\.txt"'$'` ; do
#echo $txtfile 
cat $txtfile | `find ${system} -name '*.txt'`  >  ClassA.txt
done

i dont want to display the path of $txtfile as shown in the code above rather i would like to append the vale of $txtfile before each .txt file

unfortunately the code above isnt working, am getting permession denied error!
# 7  
Old 04-27-2011
something to start with:
Code:
find . -type f -name '*.txt' | xargs -I {} -i ksh 'echo //$(dirname {}); cat {}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add coordinates to line of output extracted from input file

I am trying to compare/confirm the output of an script using the perl below, which does execute. However I can not seem to print $1 and $2 in each line of the input separated by a tab in each line of the output. The input file is quite large so I have only included a portion, but the format is the... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Compare two unsorted unequal files extracted from xml

I have two files for comparison which are extracts from set of xml files. file1 has: Comparing File: BRCSH1to320140224CC3.xml :: TZZZ:BR :: TAZZ:OUT UIZZ:0 :: ERAZ:1.000000 UIZZ:0 :: CTZZ:B UIZZ:0 :: CCAZ:MYR Comparing File: BRMY20140224CC18REG013SPFNSY13.xml :: TZZZ:BR :: TAZZ:INB... (1 Reply)
Discussion started by: vamsi gunda
1 Replies

3. Shell Programming and Scripting

Store the name of an extracted file to a temp file

What would be the best way to store the name of an extracted file from a tar to a text file? I want to extract one file from a tar and store the name of the extracted file to a temp file. tar -xvf tar_file.tar file_to_be_extracted (1 Reply)
Discussion started by: erin00
1 Replies

4. Shell Programming and Scripting

Request for advise on how to remove control characters in a UNIX file extracted from top command

Hi, Please excuse for posting new thread on control characters, I am facing some difficulties in removing the control character from a file extracted from top command, i am able to see control characters using more command and in vi mode, through cat control characters are not visible ... (8 Replies)
Discussion started by: karthikram
8 Replies

5. Programming

[Solved] Cannot execute file that include files

Iam trying to execute a file that include many files but it seems my main copy.c can't read anyone of them ----------------------------------------------------------------------------------------- Copy.c #include <sys/stat.h> #include <fcntl.h> #include "tlpi_hdr.h" #ifndef BUF_SIZE /*... (2 Replies)
Discussion started by: fwrlfo
2 Replies

6. Shell Programming and Scripting

unrar - get the name of extracted file

If I have a script that is using unrar e file.part1.rar How does the script get the name of the extracted file if I don't know the extension of the file? In my example the name would be file.***, but I wouldn't know the extension. ---------- Post updated at 05:13 PM ---------- Previous... (0 Replies)
Discussion started by: locoroco
0 Replies

7. Shell Programming and Scripting

Make new files based on the number extracted

Hi All, I have a big file which looks like this: 0 4.5 6.7 0 3.4 6 1 5 6 1 6 4 2 9 4.44 Above is just a miniature version of the file. In fact, considering the first column of the file that is 0 0 1 1 2, the numbers in actual go until 10442. This means my actual file looks like... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

8. Shell Programming and Scripting

make file (include files path)

Hi All, In make file i want to include header files from my local directory and if it did not find in local directory i want to include from network directory. can any help me how i can do this?. here is the code INCLUDE=${include}/ this is point to network dir how i can add option that it... (1 Reply)
Discussion started by: goraya430
1 Replies

9. Shell Programming and Scripting

Error running a .sh script when extracted from a jar file.

I am trying to run a script called install.sh as follows: I get a jar file, and extract it using the command: unzip filename.jar -D path/to/files then I navigate to that directory where I extracted the files, and run the command: sh install.sh (where install.sh is one of the files... (12 Replies)
Discussion started by: albert_newton
12 Replies

10. UNIX for Dummies Questions & Answers

To send an email with the body content extracted from a file

Hi, I have been trying to shoot an email with the email body to be obtained from a file. Can someone please help me with it.. I have been trying to use the MAILX commad for the same. mailx -s "test email" -r sender@test.com < file.txt but it sends the file as an attachment,while i... (3 Replies)
Discussion started by: rohit.shetty84
3 Replies
Login or Register to Ask a Question