Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
MYPATH=/var/www/html/error_logs/
TOTALFILE=$(ls $MYPATH* | wc -l)
FILETIME=$(stat --format=%y $MYPATH* | head -5 | cut -d'.' -f1)  
FILE=$(ls -1tcr $MYPATH* | head -5 | rev | cut -d/ -f1 | rev)
TOPLINE=$(head -1 $MYPATH* | grep -Po '".*?"' | head -5)

how can i elegantly print this out 5 files information into columns with headers?

FILE CREATED TIME   | FILE NAME        | ERROR HEADER
---------------------------------------------
$FILETIME           | $FILE            | $TOPLINE
2012-11-29 11:27:45 | 684939947465     | "SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)"

and so on 5 files

total files: $TOTALFILE

is there any easy way to get what i want?

note: this output i got when echo every variable

2012-11-29 11:27:45 2012-11-29 11:27:41 2012-11-28 23:33:01 2012-11-26 10:23:37 2012-11-19 22:49:36
684939947465 1313307654813 1311411049509 1234980770182 354797376843
"SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)" "SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)" "Connection to localhost:6379 failed: Connection refused (111)" "An error occurred connecting to Redis." "SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)"
share|improve this question
    
You got three answers to your question, which all address the things you ask. You should be much more precise in what you want to get a specific answer. –  Bernhard Dec 24 '12 at 12:13
add comment

3 Answers 3

up vote 5 down vote accepted

You can use the shell command 'column' for that...

column MAN page

Combine this with a loop and you're in business

#!/bin/sh

MYPATH=/
TOTALFILE=$(ls $MYPATH/* | wc -l)
FILE=$(ls -1tcr $MYPATH/* | head -5 | rev | cut -d/ -f1 | rev)

declare -a FILES
declare -a FILETIME

OUTPUT="FILENAME CREATED TIME ERROR_HEADER\n\n------------------------------ ----------------------------- ----------------------------------- ------$

for i in $MYPATH/*;
do
    FILES[${#FILES[@]}]="$i"
    FILETIME[${#FILETIME[@]}]=$(stat --format=%y $i | head -5 | cut -d'.' -f1)
    TOPLINE=$(head -1 $i | grep -Po '".*?"' | head -5)

    OUTPUT="$OUTPUT\n${FILES[${#FILES[@]}-1]} ${FILETIME[${#FILETIME[@]}-1]} $TOPLINE\n"
done

echo -ne $OUTPUT | column -t
share|improve this answer
add comment

I would recommend using printf

printf "%-30s | %-30s | %-30 " $FILETIME $FILE $TOPLINE

Where %-30s means to reserve 30 characters for the input argument of type string. The - denotes left alignment.

share|improve this answer
    
it's giving error : -bash: printf: -3: invalid option –  Rahul Patil Dec 23 '12 at 19:07
    
@RahulPatil I'm sorry, misplace the % –  Bernhard Dec 23 '12 at 19:20
    
You need to quote all of your expansions, or this will easily break. –  Chris Down Dec 23 '12 at 20:24
add comment

I would go with a a loop

printf " %-20s | %-20s | %-20s\n " FILE\ CREATED\ TIME FILE\ NAME ERROR\ HEAD
for i in "$MYPATH"/*
do
    printf "%-20s | %-20s | %-20s\n " $FILENAME $FILE $TOPLINE
done
printf "Total Files: %s" $TOTALFILES 
share|improve this answer
    
I gave you the how not the solution. Now you need to adapt your code so that the FILENAME FILE and TOPLINE are taken care inside of the loop instead of doing it before the loop. –  BitsOfNix Dec 23 '12 at 17:22
    
You need to quote all of your expansions, or this will easily break. –  Chris Down Dec 23 '12 at 20:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.