Modify print of command "ls -l -R"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify print of command "ls -l -R"
# 1  
Old 02-03-2011
Modify print of command "ls -l -R"

Hi to all,

Some help please, I want to modify using awk the print showed by command "ls -l -R" from:


Code:
/home/user1/Mydocs/Year/2010/July/Experiments:
total 1608
-rw-r--r-- 1 user1 user1    1431 Feb  2 22:45 Experiments1.TAR
-rw-r--r-- 1 user1 user1     923 Feb  2 22:45 Tests_Exp1.TXT
-rw-r--r-- 1 user1 user1    1011 Feb  2 22:45 Intro.TXT

/home/user1/Mydocs/Year/2010/July/Day_9/Other_Docs:
total 8
-rw-r--r-- 1 user1 user1 831 Feb  2 23:55 Optional_Exp1.TXT
-rw-r--r-- 1 user1 user1  73 Feb  2 23:55 Intro_Op_Exp1.TXT

to

Code:
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  02-03-11 22:44   July/
        0  02-03-11 22:45   July/Experiments/
     1431  02-03-11 22:45   July/Experiments/Experiments1.TAR
      923  02-03-11 22:45   July/Experiments/Tests_Exp1.TXT
     1011  02-03-11 22:45   July/Experiments/Intro.TXT
        0  02-03-11 23:54   July/Day_9/
        0  02-03-11 23:55   July/Day_9/Other_Docs/
      831  02-02-11 23:55   July/Day_9/Other_Docs/Optional_Exp1.TXT
       73  02-03-11 23:55   July/Day_9/Other_Docs/Intro_Op_Exp1.TXT

I'm don't know how to begin.

Any help would be appreciated.

Thanks in advance.
# 2  
Old 02-03-2011
here's the start:
ls -lR | nawk -f cg.awk
cg.awk:
Code:
BEGIN {
  FLDsize=5
  FLDdate1=6
  FLDdate2=7
  FLDtime=8

  CHRslash="/"
  format="%10s%10s%10s\t%-20s\n"
}
FNR==1 {
    printf(format, "Length", "Date", "Time", "Name")
    printf(format, "------", "----", "----", "----")
}
$0 ~ ("^" CHRslash) {pathN=split($0, pathA,"/");next}
/^total/ {next}
NF { printf(format, $FLDsize, $FLDdate1,$FLDdate2, pathA[pathN-1] CHRslash $NF)}

# 3  
Old 02-03-2011
Hi vgersh99, many thanks for your reply.

I've tested your script, the output is slightly different, but I'm not sure how to modify it to reach the desired output.

If I apply your script to the following input:

Code:
/home/user1/MainFolder/:
total 8
drwxr-xr-x 2 user1 user1 4096 2011-02-03 20:08 Folder_1
drwxr-xr-x 3 user1 user1 4096 2011-02-03 20:09 Folder_2

/home/user1/MainFolder/Folder_1:
total 5112
-rw-r--r-- 1 user1 user1 1915873 2010-09-14 15:05 Song1.ogg
-rw-r--r-- 1 user1 user1 3314295 2010-09-14 15:05 Song2.ogg

/home/user1/MainFolder/Folder_2:
total 8192
-rw-r--r-- 1 user1 user1 1684118 2010-12-29 15:17 Document_1.7z
-rw-r--r-- 1 user1 user1 3408014 2010-12-29 15:16 Document_2.7z
-rw-r--r-- 1 user1 user1 3284518 2010-12-29 15:16 Document_3.7z
drwxr-xr-x 2 user1 user1    4096 2011-02-03 20:09 SubFolder1

/home/user1/MainFolder/Folder_2/SubFolder1:
total 20
-rw-r--r-- 1 user1 user1 17013 2011-02-03 16:21 Document_4.txt

the output is:
Code:
Length      Date      Time    Name                
    ------      ----      ----    ----                
      40962011-02-03     20:08    MainFolder/Folder_1 
      40962011-02-03     20:09    MainFolder/Folder_2 
   19158732010-09-14     15:05    MainFolder/Song1.ogg
   33142952010-09-14     15:05    MainFolder/Song2.ogg
   16841182010-12-29     15:17    MainFolder/Document_1.7z
   34080142010-12-29     15:16    MainFolder/Document_2.7z
   32845182010-12-29     15:16    MainFolder/Document_3.7z
      40962011-02-03     20:09    MainFolder/SubFolder1
     170132011-02-03     16:21    Folder_2/Document_4.txt

Is missing the real path, all files appear within MainFolder.
The desired output is:

Code:
 Length      Date      Time    Name                
    ------      ----      ----    ----                
             0-02-03     20:08    MainFolder/
             0-02-03     20:08    MainFolder/Folder_1 
   19158732010-09-14     15:05    MainFolder/Folder_1/Song1.ogg
   33142952010-09-14     15:05    MainFolder/Folder_1/Song2.ogg
             0-02-03     20:09    MainFolder/Folder_2 
   16841182010-12-29     15:17    MainFolder/Folder_2/Document_1.7z
   34080142010-12-29     15:16    MainFolder/Folder_2/Document_2.7z
   32845182010-12-29     15:16    MainFolder/Folder_2/Document_3.7z
             0-02-03     20:09    MainFolder/Folder_2/SubFolder1
     170132011-02-03     16:21    MainFolder/Folder_2/SubFolder1/Document_4.txt

How to reach this goal?

Many thanks in advance
# 4  
Old 02-03-2011
Code:
cd /home/user1/Mydocs/Year/2010/
find . -type f -exec ls -l {} \;|awk '{print $5,$6,$7,$8}' OFS="\t"


Last edited by rdcwayx; 02-03-2011 at 11:08 PM..
# 5  
Old 02-04-2011
Many thanks rdcwayx,

Your script has an output more similar as what I want.

I'll try to try and work with this.

Many thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Help -- Modify awk command to handle "," delimiter

Currently I am using this command to split a CSV file based on the distinct values in the 16th (position) column. awk -F, 'NR==1 { hdr=$0; next } $16 != prev { prev=name=$16; gsub(/_]/,"",name); $0 = hdr "\n" $0 } { print > ("/Directory/File."name".Download.csv") }'... (3 Replies)
Discussion started by: lojkyelo
3 Replies

4. Shell Programming and Scripting

Modify or react on "cd" command

Hi folks, I found a pretty nice thread in this forum showing how to change the window title of a PuTTY window from within the shell. (Which I can't reference due having less than 5 postings?!?) I modified the solution a bit to have a function in my zshrc "wt()" changing the window title each... (8 Replies)
Discussion started by: mephistho
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Solaris

How to check "faulty" or "stalled" print queues - SAP systems?

Hi all, First off, sorry for a long post but I think I have no other option if I need to explain properly what I need help for. I need some advise on how best to check for "faulty" or "stalled/jammed' print queues. At the moment, I have three (3) application servers which also acts as print... (0 Replies)
Discussion started by: newbie_01
0 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

In ksh shell command - Print "-ABC" is giving error

Hi Guys, while executing the following command : print "-ABC" is giving following error : ksh: print: bad option(s) I cannot use echo for some other reasons, so any other option ? (2 Replies)
Discussion started by: sagarjani
2 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Advanced & Expert Users

Command similar to "touch" for modify File size

Hi All, I'm trying to find a command like similar to "touch" which would let me change the file size property. For ex: I have a file of size 1MB using the command i would like to set/update the size something like 1KB. Is it possible? Is there any such command which would accomplish this... (3 Replies)
Discussion started by: sriharshareddyk
3 Replies
Login or Register to Ask a Question