Operating System - HP-UX
1827673 Members
3570 Online
109967 Solutions
New Discussion

heading of file name and its contents

 
SOLVED
Go to solution
Jeeshan
Honored Contributor

heading of file name and its contents

I have some files in severals home directory. Location is like $HOME/back/.hk/
And files are created there periodically. I want the first 22 character as a heading and read its contents like table view.

How can i check it like that using shell script.
a warrior never quits
18 REPLIES 18
Hein van den Heuvel
Honored Contributor

Re: heading of file name and its contents

Ashan, please clarify the need a bit more.

Perhaps provide a reply with an TEXT attachment of a (partial) sample ls -l input listing with an example selection string and (partial!) example the desired output.

What does 'table view' mean for you?
Should the output be the whole file, or limited in length or width, or paged? (more)

Are those 22 bytes just for 'basename' or the whle path?

hth,
Hein.

perl -e "while ($f=) { printf qq(\n\n%sn\n),substr($f,0,22); open F,qq(<$f); print while($_= and $. < 10); close F }"

Jeeshan
Honored Contributor

Re: heading of file name and its contents

my file names are like bellow

20_May_09_Wed_20:52:19....txt

suppose i have 20-30 files a day and the date & time is i. e."20_May_09_Wed_20:52:19"

and every file has 3-9 lines. Suppose i want to see 20 May 09 files and it will list all 20May09 files and listing like this

Date & Time File content
--------------------- -----------------------
20_May_09_Wed_20:52:19





>>Are those 22 bytes just for 'basename' or the whle path?

yeah base name.
a warrior never quits
Hein van den Heuvel
Honored Contributor

Re: heading of file name and its contents

Try something like:

Tested:

$ perl -e 'while ($f=<*.txt>) {print (($i++)? qq(\n\n): qq(Date & Time File content\n)); open F,qq(<$f); $f=~s/(.{22}).*/$1/; print qq($f $_) while()}'

Of course you probably don't want to use a 'one-liners' but script it up with a bit more fluff (like the basename)

Untested:

use strict;
use warning;
while ($f=<*.txt>) {
print (($i++)? qq(\n\n): qq(Date & Time File content\n));
open F,qq(<$f);
$f=~s/(.{22}).*/$1/; # or substr($f,0,22)
print qq($f $_) while()
}

Hein.

James R. Ferguson
Acclaimed Contributor

Re: heading of file name and its contents

Hi:

Hein's full script needs three minor fixes. Afterall, as he noted, it was "untested".

First, a "shebang" line should be declared: '#!/usr/bin/perl'.

The 'warning' pragma should be 'warnings'.

The 'strict' pragama requires that variables be predeclared and that barewords (those without a sigil [$,@,%]) are context-sensitive). Using 'strict' allows Perl's compilation phase to catch coding errors that might be hard to otherwise find. Using 'warnings' exposes mistakes like declaring but never using a variable or using numeric comparison operators on strings.

THe 'my' list preclares all of the variables used in this script.

Thus, the first five lines of Hein's script should look like:

#!/usr/bin/perl
use strict;
use warnings;
my ( $f, $i );
while ($f=<*.txt>) {

...

Regards!

...JRF...
Jeeshan
Honored Contributor

Re: heading of file name and its contents

my script is like the bellow
#!/bin/sh
print "Enter Date\t:"
read dt
print "Enter Month\t:"
read mn
print "Enter Year\t:"
read yr

vr=$(ll $dt"_"$mn"_"$yr*|awk '{print $9}')
vn=$(echo $vr|cut -c1-22)
print "Do you want to see logs from[Y/N]: $dt"_"$mn"_"$yr ?"
read RS in
if [ "${RS}" = "Y" -o "${RS}" = "y" ]
then
for i in $vr
do
echo ""
echo ""
printf "%$Col_1 %$Col_2 \n" \
DateandTime What 2>/dev/null
printf "%$Col_1 %$Col_2 \n" \
----------------------- ------------------------------------------------------------------------------------------ 2>
/dev/null
printf "%-22s %90s\n"$vn`cat $i`
#cat $i
done
elif [ "${RS}" = "N" -o "${RS}" = "n" ]
then
print "Exiting........"
sleep 1
fi
}
aud

and i need the output as the attached way.
a warrior never quits
James R. Ferguson
Acclaimed Contributor

Re: heading of file name and its contents

Hi:

WHile I think Hein offered you a perfectly good soultion, I see you have presented a shell script of your own. Good.

If I understand your last post, your principal problem is in reading and formatting a file's contents. IF that's the case, consider something like this:

function readfile
{
typeset FILENAME=$1
typeset -i LINENO=0
while read LINE
do
if (( LINENO < 1 )); then
printf "%-22s %s\n" "${FILENAME}" "${LINE}"
else
printf "%-22s %s\n" "" "${LINE}"
fi
LINENO=$(( ${LINENO}+1 ))
done < ${FILENAME}
}

To use this function, simply call it with the name of the file, like:

readfile myfilename

Regards!

...JRF...
Jeeshan
Honored Contributor

Re: heading of file name and its contents

thanks JRF for your input.

My main objective is to read the input of file name range from user and search the files in a directory and cut the first 22 character from file name and display the content of the files to stdout or in a csv format.
a warrior never quits
James R. Ferguson
Acclaimed Contributor

Re: heading of file name and its contents

Hi (again) Ahsan:

> My main objective is to read the input of file name range from user and search the files in a directory and cut the first 22 character from file name

OK, consider doing something like this:

...
cd /path_of_interest
NAME=hk
FILES=$(find . -type f -name "${NAME}*"|awk '{print substr($0,3,22)}')
...

This code would recursively search the directory you specify; look for _files_ whose basename matches ${NAME}* and snip the first 22-characters (or less) of the name. The file name list is placed in 'FILES'.

The 'cd' first and the ure of the current directory (the '.') means that what is returned begins with './'. THus, we can begin snipping the basename at the next or 3rd position of the string.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: heading of file name and its contents

Hi Ahsan:

WHat I meant to suggest in my last post was to leverage what I had already suggested:

# cat ./tableit
#!/usr/bin/sh
function readfile
{
typeset FILENAME=$1
typeset SHORTNAME=$(echo ${FILENAME}|awk '{print substr($0,3,22)}')

typeset -i LINENO=0
while read LINE
do
if (( LINENO < 1 )); then
printf "%-22s %s\n" "${SHORTNAME}" "${LINE}"
else
printf "%-22s %s\n" "" "${LINE}"
fi
LINENO=$(( ${LINENO}+1 ))
done < ${FILENAME}
}

cd $HOME/back
NAME=hk
FILES=$(find . -type f -name "${NAME}*")

for NAME in ${FILES}
do
readfile "${NAME}"
done

...

Regards!

...JRF...
Jeeshan
Honored Contributor

Re: heading of file name and its contents

Sorry JRF No hope.

I think you didn't understand my queries.

i have several users account in my unix box. and in home directory there are some files created every day. When i pass the input argument to the script it will least down the files of a certain directory of users home directory automatically by date wise and show me the output in a formatted display. and the format was in my previous posts attachement.
a warrior never quits
James R. Ferguson
Acclaimed Contributor
Solution

Re: heading of file name and its contents

Hi (again) Ahsan:

> Sorry JRF No hope.

Really!?

> I think you didn't understand my queries.

You are correct. Your problem definition lacks details.

> i have several users account in my unix box. and in home directory there are some files created every day. When i pass the input argument to the script it will least down the files of a certain directory of users home directory automatically by date wise and show me the output in a formatted display. and the format was in my previous posts attachement.

I am not writing a whole script for you. I am attempting to guide you to a solution based on what I see you have attempted thus far.

The script you posted appears to be incomplete and untested.

Why redirect STDERR at line-22? There should be no need for a properly written 'printf' statement.

Line-32 (the closing curly brace) doesn't have an opening brace to match.

Line-33 doesn't reference anything.

Line-23 (to list a file's contents) will not produce what you expect!

This notation:

# printf "%-22s %90s\n"$vn`cat $i`

...is full of errors! Your aim was to use it to list the contents of the file according to your attached format.

First, the statement needs whitespace between its arguments:

# printf "%-22s %90s\n" $vn `cat $i`

...this "improves" the output a bit, but leaves the value of $vn and the first line of the file joined on the first output line. Each successive output line, though, consists of the _first_ and the _second_ "word" of the file's contents thans to the automatic split the shell does on its IFS (whitespace, here). To fix that, you must quote like this:

# printf "%-22s %60s\n" $vn "`cat $i`"

..but that still leaves the first line a line from the file joined to your $vn value with subsequent lines fully left-justified.

That aside, this is wasteful shell programming. You don't need to create a 'cat' process to read a file. Instead you could have done:

# printf "%-22s %60s\n" $vn "$(< $i)"

...which is more efficient than you wrote but still not what you want.

This leads to why I originally posted the 'readfile()' function I used. It accomodates your basic formatting needs. I subsequently showed how you might use it to process a list of file(names) to produce your output. If files of specific dates are what you want to select, then you need to consider having the 'find' return lines of 'ls' output or simply generate a list based on an 'ls' itself. Once again, your requirements definitions are scanty.

Which brings us full-circle:

> Sorry JRF No hope.

Really?

Regards!

...JRF...
Jeeshan
Honored Contributor

Re: heading of file name and its contents

Thanks JRF.

Sorry, I mistakenly mention that, I was unable to describe you my problem.

Actually i want to optimize the script later on, while I'm finishing it what I actually get. This time script efficiency is not a big issue.

Please find modified script bellow. Its output is ok in a sense that, the content of file is coming but left justified and only one file heading comes. not all file system comes as I attached the format earlier.

#!/usr/bin/sh

print "Enter Date\t:"
read dt
print "Enter Month\t:"
read mn
print "Enter Year\t:"
read yr

function aud {

Col_1="-22s"
Col_2="-90s"

vr=$(ll $dt"_"$mn"_"$yr*|awk '{print $9}')
vn=$(echo $vr|cut -c1-22)
#ct=$(cat $vn)
print "Do you want to see logs from[Y/N]: $dt"_"$mn"_"$yr ?"
read RS in
if [ "${RS}" = "Y" -o "${RS}" = "y" ]
then
echo $vr | while read line
do
echo ""
echo ""
printf "%$Col_1 %$Col_2 \n" \
DateandTime What 2>/dev/null
printf "%$Col_1 %$Col_2 \n" \
----------------------- ------------------------------------------------------------------------------------------ 2>
/dev/null
printf "%22s %-90s\n" $vn "`cat $line`"
done
elif [ "${RS}" = "N" -o "${RS}" = "n" ]
then
print "Exiting........"
sleep 1
fi
}
aud
a warrior never quits
James R. Ferguson
Acclaimed Contributor

Re: heading of file name and its contents

Hi (again) Ahsan:

> Please find modified script bellow. Its output is ok in a sense that, the content of file is coming but left justified and only one file heading comes. not all file system comes as I attached the format earlier.

Yes it is. You need to look again at my post on May 25, specifically the readfile() function. Using that as a guide, your script could be modified like this:

#!/usr/bin/sh
print "Enter Date\t:"
read dt
print "Enter Month\t:"
read mn
print "Enter Year\t:"
read yr
function aud {
typeset LINENO=0
vr=$(ll $dt"_"$mn"_"$yr*|awk '{print $9}')
vn=$(echo $vr|cut -c1-22)
print "Do you want to see logs from[Y/N]: $dt"_"$mn"_"$yr ?"
read RS in
if [ "${RS}" = "Y" -o "${RS}" = "y" ]; then
while read line #...read the file
do
if (( LINENO < 1 )); then
echo "\n\n"
printf "%22s %-90s\n" DateandTime What
printf "%22s %-90s\n" \
"---------------------- ------------------------------------------------------------------------------------------"
printf "%22s %-90s\n" ${vn} "${line}"
else
printf "%22s %-90s\n" "" "${line}"
fi
LINENO=$(( ${LINENO}+1 ))
done < ${vr}
elif [ "${RS}" = "N" -o "${RS}" = "n" ]; then
print "Exiting........"
sleep 1
fi
}
aud

...

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: heading of file name and its contents

Ahsan:

Have all your questions been answered?

Regards!

...JRF...
Jeeshan
Honored Contributor

Re: heading of file name and its contents

JRF, I was busy on another project.

Sorry, still i didn't get the desired output. after modifying script according to you last time the output is gibberish.

My last script's output is coming fine but it only shows the first file name is name and date column and in what column it shows all files content but left justified.

a warrior never quits
James R. Ferguson
Acclaimed Contributor

Re: heading of file name and its contents

Hi Ahsan:

Sorry, here's a better cut of what I think you want, preserving as much of your logic as I can.

#!/usr/bin/sh
print "Enter Date\t:"
read dt
print "Enter Month\t:"
read mn
print "Enter Year\t:"
read yr
function aud {
typeset -l RS
typeset LINENO=0
vr=$(ll $dt"_"$mn"_"$yr*|awk '{print $9}')
print "Do you want to see logs from[Y/N]: $dt"_"$mn"_"$yr ?"
read RS X
[ "${RS}" = "y" ] || { echo "exiting..."; sleep 1; return; }
printf "\n\n%22s %-90s\n" DateandTime What
printf "%22s %-90s\n" \
"---------------------- ------------------------------------------------------------------------------------------"
for FILE in ${vr}
do
vn=$(echo ${FILE}|cut -c1-22)
while read LINE #...read the file
do
if (( LINENO < 1 )); then
printf "%22s %-90s\n" ${vn} "${LINE}"
else
printf "%22s %-90s\n" "" "${LINE}"
fi
LINENO=$(( ${LINENO}+1 ))
done < ${FILE}
echo "\n"
LINENO=0
done
}
aud

...

Regards!

...JRF...


Jeeshan
Honored Contributor

Re: heading of file name and its contents

Oh, awesome man, you are mentor.

That works.

Another concern, if I want the csv format of the output as shown in stdout, then what else i need to do?

Hope to get help from you later on also.
a warrior never quits
James R. Ferguson
Acclaimed Contributor

Re: heading of file name and its contents

Hi (again):

> if I want the csv format of the output as shown in stdout, then what else i need to do?

If you mean that the contents of your logs are comma-seperated-values, then _nothing_. Try it.

Regards!

...JRF...