include file name to extracted files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting include file name to extracted files
# 8  
Old 04-27-2011
Quote:
Originally Posted by 2pugs
Code:
    for txtfile in $(ls $system/*.txt)

That's a rather pointless construct. It's inefficient (requires fork-exec to create a subshell and run ls), prone to breakage if any of the file names contain an IFS character (by default, space, tab, and newline), and is bound by the system's exec()'s limit (ARG_MAX).

A simpler, safer, more efficient alternative: for txtfile in "$system"/*.txt

Regards,
Alister

Last edited by alister; 04-27-2011 at 06:17 PM..
# 9  
Old 04-28-2011
Thank you all for ur efforts, unfortunately none of the suggested solutions has worked for me.
I managed to get the file name, the question is how to inser the file name before the concatenation happens.


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

generated txtfile: no such file or directory


Quote:
Code:
find "$system" -name '*.txt' -exec sh -c "echo '# {}';cat {}" \;

has generated missing argument to -exec


Quote:
Code:
find . -type f -name '*.txt' | xargs -I {} -i ksh 'echo //$(dirname {}); cat {}'

xargs: ksh: No such file or directory
# 10  
Old 04-28-2011
Quote:
Originally Posted by miss_dodi
Thank you all for ur efforts, unfortunately none of the suggested solutions has worked for me.
I managed to get the file name, the question is how to inser the file name before the concatenation happens.




generated txtfile: no such file or directory



has generated missing argument to -exec




xargs: ksh: No such file or directory
what OS are you on?
Try:
Code:
find . -type f -name '*.txt' | xargs -I {} -i /bin/ksh 'echo //$(dirname {}); cat {}'

# 11  
Old 04-28-2011
Linux Ubuntu 10.04.2
# 12  
Old 04-28-2011
Hi,

If i understand what you want, and if there is no IFS characters on filenames and dirnames, this could do the trick.

In my example, i suppose that all dirname with txt files to cat are named Class*.
But you can remove the -name 'Class*' to include all dirs.

Code:
#!/bin/bash
echo "Let's do the job . . ."
for txtdir in $(find . -type d -name 'Class*'); do
    # Remove old output file.
    rm $txtdir.out &>/dev/null
    for txtfile in $(find $txtdir -name '*.txt'); do
        [ -e $txtdir.out ] && echo "Processing $txtdir.out . . ."
        echo "  Adding $(basename $txtfile)."
        echo "// $(basename $txtfile)" >>$txtdir.out
        cat $txtfile >>$txtdir.out
    done
done
echo "Done."

# 13  
Old 04-28-2011
Hello, miss_dodi:

Unless I missed it, you never made it clear if the output filename varies with directory and if so how to choose its name. To collect the desired concatenation of all .txt files in a directory, with each file's contents preceded by its filename, the solution that follows will create a file named ALL-TEXT-FILES.txt in each directory

You mention that you have 145 directories, but neglected to explain how the code is expected to visit them. Do you have a list to feed the script, either via a pipe or command line arguments? Or are they all in a hierarchy which can be simply traveresed with find from a single root location? I will assume the later and the following script can take a single argument, the location of this starting directory. If absent, the current working directory is assumed.

Code:
#!/bin/sh

find "${1:-.}" -type d -exec sh -c '
        for d; do
                out=$d/ALL-TEXT-FILES.txt
                for f in "$d"/*.txt; do
                        { [ -f "$f" ] && [ -r "$f" ]; } || continue
                        printf "//%s\n" "${f##*/}" >> "$out"
                        cat "$f" >> "$out"
                done
        done 
' sh {} +

I tested it and it works as I intend.

However, there is a bug in this code (which is also present in some of the other suggestions). It's unlikely to be triggered, but it's lurking ... sleeping ... hoping. Smilie

In case anyone would prefer to find it themselves...

***** CAUTION: SPOILERS AHEAD *****





If a directory happens to contain a file whose name is identical to the output file, cat will enter an infinite loop of reading-writing to itself until the machine explodes. The non-lazy solution would be to use a unique tempfile (or at least a filename that is guaranteed to be outside the traversal).

Regards,
Alister

Last edited by alister; 04-28-2011 at 02:28 PM..
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