Trying to get 5th field from ls -l output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to get 5th field from ls -l output
# 1  
Old 08-01-2014
Trying to get 5th field from ls -l output

Bash Shell/Oracle Linux 6.4

Following is an ls -lh output. Files which are equal to or higher than 1Gigabytes will displayed with G in the 5th column of the output.
I am trying to find all files which are equal to or higher than 1G

File sizes are shown in the 5th column. So, I tried
Code:
cut -d' ' -f5

. But it returned blank lines, so I think it may be counting each individual space as a field when there is more than one space together


Code:
ls -lrth *.dmp | cut -d' ' -f5

-rwxrwxrwx 1 migration gdma       32K Mar 15 17:38 MIG_DUMPFILE_POL_20140315_173834.dmp
-rw-r----- 1 oracle    asmadmin  1.7G Mar 17 09:27 MIG_DUMPFILE_OM411_20140317_091908.dmp
-rw-r----- 1 oracle    asmadmin   28K Mar 20 15:36 MIG_DUMPFILE_AI317_20140320_153623.dmp
-rw-r----- 1 oracle    asmadmin   74M Mar 20 16:55 MIG_STG_UIM_MSISDN_TGT_NM423_20140320_165437.dmp
-rw-r----- 1 oracle    asmadmin   68M Mar 20 17:02 MIG_STG_UIM_MSISDN_TGT_NM424_20140320_170224.dmp
-rw-r----- 1 oracle    asmadmin   92M Mar 20 17:11 MIG_STG_UIM_MSISDN_TGT_NM425_20140320_171042.dmp
-rw-r----- 1 oracle    asmadmin  231M Mar 21 16:14 MIG_DUMPFILE_STG_LKP_BSS_BA_20140321_161411.dmp
-rw-r----- 1 oracle    asmadmin   93M Mar 21 16:34 MIG_DUMPFILE_OM412_20140321_163354.dmp
-rw-r----- 1 oracle    asmadmin  446M Mar 21 20:00 MIG_DUMPFILE_AM410_20140321_195913.dmp
-rw-r----- 1 oracle    asmadmin  146M Mar 21 20:08 MIG_DUMPFILE_AM411_20140321_200815.dmp
-rw-r----- 1 oracle    asmadmin  319M Mar 21 20:12 MIG_DUMPFILE_STG_LKP_BSS_CA_20140321_201126.dmp
-rw-r----- 1 oracle    asmadmin   28K Mar 23 14:27 MIG_DUMPFILE_AI317_20140323_142700.dmp
-rw-r----- 1 oracle    asmadmin   36K Mar 24 09:53 MIG_DUMPFILE_TEST_20140411_060116.dmp
-rwxrwxrwx 1 migration gdma      537M Mar 24 10:20 MIG_DUMPFILE_LKP_ast_20140324_102003.dmp
-rwxrwxrwx 1 migration gdma       56K Mar 24 10:33 MIG_DUMPFILE_LKP_LTE1_20140324_103354.dmp
-rwxrwxrwx 1 migration gdma       56K Mar 24 10:34 MIG_DUMPFILE_LKP_LTE2_20140324_103429.dmp
-rw-r----- 1 oracle    asmadmin  1.4G Mar 26 00:36 MIG_DUMPFILE_AM414_20140326_003118.dmp
-rw-r----- 1 oracle    asmadmin   42M Mar 26 01:45 MIG_DUMPFILE_SR414_20140326_014525.dmp
-rw-r----- 1 oracle    asmadmin  475M Mar 26 02:24 MIG_DUMPFILE_OM410_20140326_022241.dmp
-rw-r----- 1 oracle    asmadmin  2.4G Mar 26 09:27 MIG_DUMPFILE_OM411_20140326_091908.dmp

Expected output:
Code:
-rw-r----- 1 oracle    asmadmin  1.7G Mar 17 09:27 MIG_DUMPFILE_OM411_20140317_091908.dmp
-rw-r----- 1 oracle    asmadmin  1.4G Mar 26 00:36 MIG_DUMPFILE_AM414_20140326_003118.dmp
-rw-r----- 1 oracle    asmadmin  2.4G Mar 26 09:27 MIG_DUMPFILE_OM411_20140326_091908.dmp

Can this be acheived without awk or sed ?
# 2  
Old 08-01-2014
Quote:
Originally Posted by John K
Can this be acheived without awk or sed ?
Is there any reason for NOT using sed or awk? I often see such requests for home works?
# 3  
Old 08-01-2014
Code:
ls -lrht *.dmp | awk '$5 ~ /G/'

This User Gave Thanks to SriniShoo For This Post:
# 4  
Old 08-01-2014
Quote:
prvnrk:

Is there any reason for NOT using sed or awk?
Its because I don't know awk and sed Smilie

Thank you Srinishoo

Question1.
In your solution
Code:
awk '$5 ~ /G/'

, what is the role of tilde (~) character and /G/ ?


Question2
.
How did you manage to ignore the varying spaces between the fields ?

ie. If you Subtitute space with red underscores for readability as shown below, you can see 4 underscores between user and group
and 1 underscore between group and size field

Code:
-rw-r----- 1 oracle____asmadmin_1.7G Mar 17


Last edited by rbatte1; 08-04-2014 at 07:18 AM..
# 5  
Old 08-01-2014
A1: a tilde is a regex match and /pattern/ is how those are quoted, verus text which is quoted as "text".

A2:
by default, fields are separated by any amount of whitespace.
This User Gave Thanks to neutronscott For This Post:
# 6  
Old 08-01-2014
Code:
$ find -name "*.dmp" -size +1G -exec ls -lrth {} \;

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 08-01-2014
With a recent bash, you can make use of its "here strings" and get rid of awk etc:
Code:
ls -lh| while read line; do read x x x x SIZE x <<< "$line"; [ "${SIZE//[^G]}" ] && echo "$line"; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Combine Similar Output from the 2nd field w.r.t 1st Field

Hi, For example: I have: HostA,XYZ HostB,XYZ HostC,ABC I would like the output to be: HostA,HostB: XYZ HostC:ABC How can I achieve this? So far what I though of is: (1 Reply)
Discussion started by: alvinoo
1 Replies

2. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

3. Shell Programming and Scripting

sed : Second field not getting displayed in the output

Hi , I have a file (input.txt) with the below contents : 20150223T12:00:25 hostnamex 20150716T10:40:54 hostnamey 20150202T20:08:03 hostnamez I want the output to be like below i.e excluding the timestamp ( THH:MM:SS) in the above file as follows : 20150223 hostnamex 20150716... (2 Replies)
Discussion started by: rahul2662
2 Replies

4. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

5. UNIX for Dummies Questions & Answers

Ps output field extract

This is the output from ps -ef cmd . I have to extract the fourth (C) and the seventh (TIME) field root 3932344 3801216 Apr 08 - 0:00 /usr/sbin/rsct/bin/ERrmd root 3997836 1 0 Apr 08 - 0:00 /usr/sbin/uprintfd root 4128894 3801216 0 Apr 08 - 0:09... (3 Replies)
Discussion started by: Anu_1
3 Replies

6. UNIX for Dummies Questions & Answers

awk - output field separator

In awk, how do I print all fields with a specified output field separator? I have tried the following, which does not print the output FS: echo a b c d | awk 'BEGIN{OFS = ";"}{print $0}' (3 Replies)
Discussion started by: locoroco
3 Replies

7. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

8. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

9. Shell Programming and Scripting

Can we use 'tr' command to print 5th column of output of 'ls -l'

Hi All, I know awk command can do it, but can we use tr command to print 5th column of out put 'ls -l' command???? Regards, Nidhi... (4 Replies)
Discussion started by: Nidhi2177
4 Replies

10. UNIX for Dummies Questions & Answers

Retrieve 5th Field to Last Field !!

I have a script which retrieves certain fields from a text file using awk. The delimiter used is white space. cat /home/eis/boss/OPS|while read LINE do crdno=`echo $LINE | awk '{print $1}'` atm=`echo $LINE | awk '{print $2}'` seq=`echo $LINE | awk '{print $3}'` amount=`echo $LINE | awk... (3 Replies)
Discussion started by: jobbyjoseph
3 Replies
Login or Register to Ask a Question