Creating table in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating table in Unix
# 1  
Old 10-01-2009
Question Creating table in Unix

Hi All,
In a given directory, I need to list the files present in it in the below given format as a table.

 
File name Permission Number of Bytes File Type
Telecom1 --w-r-x 1230 Directory
Telecom2 ---x---x---x 450 Device file
Telecom3 ------------ 4000 Hidden file

In the above example we have 3 files in a given directory Telecom 1,2,3. 1 is a Directory 2 is a Device file and 3 is a hidden file.

Thanks

Last edited by mr_manii; 10-01-2009 at 03:58 AM..
# 2  
Old 10-01-2009
You can use awk to rearange the fields from an ls command and add some logic to determine the type of file.

For example:
Code:
ls -al | awk '
  /^total/ { next }
  {
    TYPE = "regular file"
    if ($1 ~ /^d/)
      TYPE = "directory"
    else if ($NF ~ /^\./)
      TYPE = "hidden file"
    else if ($1 ~ /^b/)
      TYPE = "block device"
    else if ($1 ~ /^p/)
      TYPE = "pipe"
 
    printf( "%-30s %14s %10d %s\n",  $NF, $1, $5, TYPE)
  }
'
.                                  drwxr-xr-x       4096 directory
..                                 drwxr-xr-x       4096 directory
file1                              -rw-r--r--        141 regular file
file2                              -rw-r--r--         30 regular file
my_logfile                         -rw-r--r--          6 regular file
myPipe                             prw-r--r--          0 pipe
T                                  -rwx------        185 regular file
tmp                                drwxr-xr-x       4096 directory
tmp2                               drwxr-xr-x       4096 directory
.x                                 -rw-r--r--          0 hidden file
Y                                  -rwx------        321 regular file

Which OS / Shell are you using? It's not clear from your permission fields how you determine what's what. (i.e. a file with ---------- does not respresent a hidden file)

Last edited by Scott; 10-01-2009 at 07:54 AM.. Reason: fixed paste error: closing apostrophie in the wrong place
# 3  
Old 10-01-2009
Hi scottn,
Thanks for the reply, the file is a hidden file starts with "." with file permission mode "000". Thats why it's showing like that.

Can we make it look like a table with the column heading as given below
File name Permission Number of Bytes File Type
# 4  
Old 10-01-2009
Quote:
Originally Posted by mr_manii
Can we make it look like a table with the column heading as given below
File name Permission Number of Bytes File Type
The output of scottns programm is controlled only by the last line "printf(....". Just change the output format to your needs and you are done (see "man printf" for details, awks printf works exactly the same way as the C function).

I hope this helps.

bakunin
# 5  
Old 10-01-2009
Code:
#!/bin/sh

ls -latr | awk '{printf ("%-20s %s %-20s \n",$9,$1,$5)}' > test_list_files

while read line
do
         file_type=`echo $line | awk '{print($2)}' | cut -c1`
         if [ -z "$file_type" ]
         then
                continue
          fi

         file_name=`echo $line | awk '{print($1)}'`
         if [ "$file_name" == "test_list_files" ]
         then
                continue
         fi

         file_permission=`echo $line | awk '{print($2)}'`
         file_size=`echo $line | awk '{print($3)}'`

         hidden_files=`echo "$line" | grep "^\."`
         if [ -n "$hidden_files" ]
         then
                printf "%-20s\n $line \t HIDDEN"
                continue
         fi

         if [ "$file_type" == "-" ]
         then
                printf "%-20s \n $line \t NORMAL FILE"
        elif [ "$file_type" == "d" ]
        then
                printf "%-20s\n $line \t DIRECTORY"
        elif [ "$file_type" == "c" ]
        then
                printf "%-20s \n $line \tCHAR DEVICE"
        elif [ "$file_type" == "b" ]
        then
                printf "%-20s \n $line  \t BLOCK DEVICE"
        fi
        hidden_files=''

done  < test_list_files

# 6  
Old 10-01-2009
to pravin27 ... bash
Code:
#!/bin/bash

ls -latr | awk '{printf ("%-20s %s %-20s \n",$9,$1,$5)}' | while read line
do
    file_type=$(echo $line | awk '{print($2)}' | cut -c1)
    [ -z "$file_type" ] && continue
    file_name=$(echo $line | awk '{print($1)}')
    file_permission=$(echo $line | awk '{print($2)}')
    file_size=$(echo $line | awk '{print($3)}')
    [ -n "$(echo "$line" | grep "^\.")" ] && { printf "%-20s\n $line \t HIDDEN"; continue; }

    case ${file_type} in
         -)  printf "%-20s \n $line \t NORMAL FILE" ;;
         d)  printf "%-20s\n $line \t DIRECTORY" ;;
         c)  printf "%-20s \n $line \tCHAR DEVICE" ;;
         b)  printf "%-20s \n $line  \t BLOCK DEVICE" ;;
    esac
done

# 7  
Old 10-01-2009
Hi

Thanks inotech

I will take care of this from next time...

Last edited by pravin27; 10-01-2009 at 07:59 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating html table from data in file

Hi. I need to create html table from file which contains data. No awk please :) In example, ->cat file num1 num2 num3 23 3 5 2 3 4 (between numbers and words single TAB). after running mycode i need to get (heading is the first line): <table>... (2 Replies)
Discussion started by: Manu1234567
2 Replies

2. UNIX for Advanced & Expert Users

Insert Command Creating Table Locking Problem

Hi, i have a java based tool which does insert operation in a TABLE, and in parallel the same table is used by my C++ code which does select Query. the Table will be always busy, but sometimes the table is getting locked when i try to make an insert, am bit confused whether the lock is... (9 Replies)
Discussion started by: senkerth
9 Replies

3. UNIX for Dummies Questions & Answers

Creating a condensed table from a pre-existing table in putty

Hello, I'm working with putty on Windows 7 professional and I'd like to know if there's a way to gather specific lines from a pre-existing table and make a new table with that information. More specifically, I'd like the program to look at a specific column, say column N, and see if any of the... (5 Replies)
Discussion started by: Deedee393
5 Replies

4. Web Development

MYSQL: Creating Dynamic Table Names 5.1

Hey everyone. Thanks for looking at this. I'm trying to create a table with the dynamic name of TableName + today's date. My variables are all happily created but the system chokes when I try to create the new table name example: Set @BFBW = CONCAT("BFBW", CURDATE()); Select @BFBW; ... (2 Replies)
Discussion started by: Astrocloud
2 Replies

5. Shell Programming and Scripting

Creating a table

I have a file like below Iter 1: Best Model = 10.0 12.0 13.0 17.0 23.3 78.7 Iter 2: Best Model = 10.0 20.0 30.0 40.0 50.0 60.0 Iter 3: Best Model = 27.3 46.3 84.5 23.0 34.5 35.4 etc I want to use a scipts using csh or awk to select the iteration number and show the numbers in a table... (2 Replies)
Discussion started by: kristinu
2 Replies

6. Programming

Creating a table like format with rows and columns

I have few files which have two columns in each. like e2 1 1 2694 2 4 2485 3 2 2098 5 1 2079 6 5 2022 9 4 1734 11 5 1585 13 2 1461 18 1 1092 21 2 1019 24 1 915 25 3 907 27 1 891 28 3 890 34 1 748 39 1 700 (1 Reply)
Discussion started by: kamuju
1 Replies

7. UNIX for Dummies Questions & Answers

Creating a table (graphic not database)

Hi, I want to create a table on our unix box that allows the user to tab through it and select certain option by putting an asterix or similair into it. e.g. -------------- |Start App | | |Stop App |*| etc... Can this be done using a script (never seen any graphics options in ksh, but... (2 Replies)
Discussion started by: dlam
2 Replies

8. Programming

Creating a Hash Table

Dear Friends, I want to create a hash table using the standard Glib header (if possible) so that I can store a structure and keep the hash key(search key) based on a string. Any example code would be great since I am not able to get the main idea. best regards Skull (4 Replies)
Discussion started by: callmetheskull
4 Replies

9. UNIX for Advanced & Expert Users

Creating a hash table using shell script

Hi, For one of my programs, I need to have a hashtable as in Perl. Unfortunately shell doesnt provide any variable like hash. Is there anyway/trick, I could implement a hash in shell (using shell scripts/sed/awk). JP (2 Replies)
Discussion started by: jyotipg
2 Replies
Login or Register to Ask a Question