- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Outputing contents of directory to a text file?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:05 AM
04-26-2006 07:05 AM
Outputing contents of directory to a text file?
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:14 AM
04-26-2006 07:14 AM
Re: Outputing contents of directory to a text file?
cd /dir
for FILE in *
do
echo ${FILE} >> /dir1/outputfile
cat ${FILE} >> /dir1/outputfile
echo "" >> /dir/outputfile
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:14 AM
04-26-2006 07:14 AM
Re: Outputing contents of directory to a text file?
Give a file of file names:
while read LINE
do
echo ${LINE}
sed -e '2p' ${LINE}
done < /tmp/filenames
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:16 AM
04-26-2006 07:16 AM
Re: Outputing contents of directory to a text file?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:17 AM
04-26-2006 07:17 AM
Re: Outputing contents of directory to a text file?
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:19 AM
04-26-2006 07:19 AM
Re: Outputing contents of directory to a text file?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:21 AM
04-26-2006 07:21 AM
Re: Outputing contents of directory to a text file?
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:21 AM
04-26-2006 07:21 AM
Re: Outputing contents of directory to a text file?
Just create a file with the desired script like
vi filename
and then run it like...
./filename
Cheers,
Himanshu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:44 AM
04-26-2006 07:44 AM
Re: Outputing contents of directory to a text file?
# ls -1 | xargs -i awk '{if(NR==1)print FILENAME;print $0}' {} > indexfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 07:52 AM
04-26-2006 07:52 AM
Re: Outputing contents of directory to a text file?
# ls -1 | xargs -i -n1 awk '{if(NR==1)print FILENAME;print $0}' > indexfile
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2006 08:45 AM
04-26-2006 08:45 AM
Re: Outputing contents of directory to a text file?
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...