Operating System - HP-UX
1829101 Members
2872 Online
109986 Solutions
New Discussion

Outputing contents of directory to a text file?

 
Joshua Goi
Frequent Advisor

Outputing contents of directory to a text file?

Hi all,

I have a question for all you HP-UX experts out there ;) Might require some creativity.. I've ran out of them for this poser :(

Anyway, I have a directory of text files. For example:
filename1
filename2
filename3

And each file contains 2 lines of text. For example:
line1
line2

What I'm trying to do here to create an index file of sorts. This file will contain all the names of the files present in this directory. And after each entry of filename, it will be followed by the 2 lines of text present in that particular file. The final contents of the index file should look like:
filename1
line1
line2
filename2
line1
line2
.
.
.

I have experimented with a few commands and so far I can't seem to get the product that I want.

For example:
# more * > index.txt
will just give me the contents of the files.

Can anyone help me?

Thanks a million!
10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: Outputing contents of directory to a text file?

A script like this should do:

cd /dir
for FILE in *
do
echo ${FILE} >> /dir1/outputfile
cat ${FILE} >> /dir1/outputfile
echo "" >> /dir/outputfile
done
James R. Ferguson
Acclaimed Contributor

Re: Outputing contents of directory to a text file?

Hi:

Give a file of file names:

while read LINE
do
echo ${LINE}
sed -e '2p' ${LINE}
done < /tmp/filenames

Regards!

...JRF...
Joshua Goi
Frequent Advisor

Re: Outputing contents of directory to a text file?

Hmmm can't say I know how to do scripts. How do I put those algorithms into a script in hp-ux, and then run them?

Bill Hassell
Honored Contributor

Re: Outputing contents of directory to a text file?

For a directory with a few dozen files, this is a simple method:

#!/usr/bin/sh
set -u
export PATH=/usr/bin

DIR=/whatever
INDEX=index.txt

cd $DIR
for MYFILE in *
do
echo $MYFILE >> $INDEX
echo " $(head -2 $MYFILE)" >> $INDEX
done

To make the index file more readable, you can insert some additional spaces in front of line1 and line2. If the directory contains thousands of files (too long for the command line), it will need some revisions.


Bill Hassell, sysadmin
Kent Ostby
Honored Contributor

Re: Outputing contents of directory to a text file?

run as:

myscript.sh $1
where $1 is the directory name

myscript.sh contains:

touch .myscript1
rm .myscript*
ll $1 | awk -v dir=$1 'NF>5{print "echo ",dir"/"$NF; print "cat ",dir"/"$NF}' >> .myscript1
chmod 755 .myscript1
./.myscript1
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Bill Hassell
Honored Contributor

Re: Outputing contents of directory to a text file?

You copy or paste the lines of the script into a file, save the file with a meaningful name and change the mode to executable using chmod 755 somefile. Then just type the name of the file. You'll need to edit the file to change the directory and/or the name of the index file.


Bill Hassell, sysadmin
Himanshu_3
Valued Contributor

Re: Outputing contents of directory to a text file?

Joshua,

Just create a file with the desired script like

vi filename

and then run it like...

./filename

Cheers,
Himanshu
Sandman!
Honored Contributor

Re: Outputing contents of directory to a text file?

Here's a one-liner awk script that would do the job:

# ls -1 | xargs -i awk '{if(NR==1)print FILENAME;print $0}' {} > indexfile
Sandman!
Honored Contributor

Re: Outputing contents of directory to a text file?

On second thoughts it might be helpful to add the "-n" switch to awk in case the total number of files in the directory of interest exceeds the system maximum limit for the argument list parameter.

# ls -1 | xargs -i -n1 awk '{if(NR==1)print FILENAME;print $0}' > indexfile

cheers!
James R. Ferguson
Acclaimed Contributor

Re: Outputing contents of directory to a text file?

Hi (again):

Sorry, the 'sed' statement to print 2-lines of the file's found should be:

# sed -e '2q' ${LINE}

Should you not want to pre-generate a file whose lines specify the filenames that you want to process, as in my first solution, yu could use this script:

# cat .mycontents
#!/usr/bin/sh
find ${1} -xdev -type f | while read LINE
do
if [ `file ${LINE} | grep -c ascii` -eq 1 ]; then
echo "[ ${LINE} ]"
sed -e '2q' ${LINE}
echo ""
fi
done
exit 0

...Run as:

# ./mycontents /pathname

This script will find all *files* in the pathname you specify. If the file found appears to be an ASCII text file, then its name is printed in square brackets followed by the first two (2) lines that comprise the file.

The output might look like this:

[ /tmp/opts ]
#!/usr/bin/perl
# use warnings;

[ /tmp/sampletext ]
line-1 abcdefghijklmnopqrstuvwxyz01234567890
line-2 abcdefghijklmnopqrstuvwxyz01234567890

Regards!

...JRF...