- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- heading of file name and its contents
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
05-21-2009 08:07 PM
05-21-2009 08:07 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 08:24 PM
05-21-2009 08:24 PM
Re: heading of file name and its contents
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=
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 08:38 PM
05-21-2009 08:38 PM
Re: heading of file name and its contents
20_May_09_Wed_20:52:19.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 09:00 PM
05-21-2009 09:00 PM
Re: heading of file name and its contents
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2009 05:15 AM
05-23-2009 05:15 AM
Re: heading of file name and its contents
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2009 11:05 PM
05-23-2009 11:05 PM
Re: heading of file name and its contents
#!/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2009 08:45 AM
05-24-2009 08:45 AM
Re: heading of file name and its contents
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2009 07:54 PM
05-24-2009 07:54 PM
Re: heading of file name and its contents
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2009 05:47 AM
05-25-2009 05:47 AM
Re: heading of file name and its contents
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2009 06:33 AM
05-25-2009 06:33 AM
Re: heading of file name and its contents
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2009 02:27 AM
05-27-2009 02:27 AM
Re: heading of file name and its contents
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2009 04:07 AM
05-27-2009 04:07 AM
Solution> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2009 09:24 PM
05-27-2009 09:24 PM
Re: heading of file name and its contents
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2009 04:21 AM
05-28-2009 04:21 AM
Re: heading of file name and its contents
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2009 04:29 PM
06-04-2009 04:29 PM
Re: heading of file name and its contents
Have all your questions been answered?
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2009 08:45 PM
06-04-2009 08:45 PM
Re: heading of file name and its contents
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2009 05:05 AM
06-05-2009 05:05 AM
Re: heading of file name and its contents
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2009 05:56 AM
06-05-2009 05:56 AM
Re: heading of file name and its contents
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2009 09:04 AM
06-05-2009 09:04 AM
Re: heading of file name and its contents
> 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...