Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract a specific word in bash

Tags:

linux

bash

sed

awk

cut

I have some lines in below forms:

-rw-r--r-- sten/sefan anonymous 8593 2011-12-05 18:28 8M
-rw-r--r-- sten/sefan 8593 2011-12-05 18:28 8M

How can I get the 8593 one-liner?

The lines are retrieved by performing some dry-run of archives, e.g.:

$ tar jtvf zip64support.tar.bz2 
-rw-r--r-- stefan.bodewig/Domain Users 16195018 2011-10-14 21:05 100k_Files.zip
-rw-r--r-- stefan.bodewig/Domain Users 14417258 2011-10-14 21:05 100k_Files_7ZIP.zip

or:

$ tar jtvf bla.tar.bz2 
-rw-r--r-- tcurdt/tcurdt   610 2007-11-14 18:19 test1.xml
-rw-r--r-- tcurdt/tcurdt    82 2007-11-14 18:19 test2.xml

Specifically to get the number in a line with YYYY-mm-dd after it.

like image 272
yuwang Avatar asked Mar 02 '26 15:03

yuwang


2 Answers

The command you are after to get the filesizes in the current directory is

$ stat -c %s *

You do not want to use bash,awk or cut to do this and your question is a great reason why as in the first line it would be the fourth column and in the second it's the third. Parsing the output of ls is not recommended!


Edit:

Since the column is number is gaurenteed I would use grep with positive lookahead:

$ tar jtvf zip64support.tar.bz2|grep -Po '[0-9]+(?= [0-9]{4}-[0-9]{2}-[0-9]{2})' 
16195018
14417258
like image 156
Chris Seymour Avatar answered Mar 05 '26 07:03

Chris Seymour


give this a try:

tar jtvf bla.tar.bz2|awk '$0=$3'

in your question you mentioned

get the number in a line with YYYY-mm-dd after it.

if you really want to do with grep:

tar ... |grep -oP '\d+(?= \d{4}-)'
like image 30
Kent Avatar answered Mar 05 '26 07:03

Kent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!