Bash: extracting 2 strings from 1 line

Hi everyone.

I am very new in bash scripting (and scripting at all).

I've got lines like these:

-rw-r--r-- 1 setub 1049089 27M mars  13  2017 arch_amiel_038g_f016r.tif
-rw-r--r-- 1 setub 1049089 584K juin   9  2008 arch_amiel_composition.jpg

I wish to extract 2 string types so that I can get:

27M[separator]arch_amiel_038g_f016r.tif
584K[separator]arch_amiel_composition.jpg

I used sed -d to try first to delete " -rw-r--r-- " but I was discouraged soon, co's I couldn't find the way out of deleting also the "r" of the expressions " arch_amiel_038g_f016r.tif " and " arch_amiel_composition.jpg ".

If you have an easy solution to get what I wish (the 2 lines above) or some suggestion to go forward...

Thanks a lot!!! :slight_smile:

Welcome to the forum.

I'm a bit surprised about your problem as sed -d doesn't yield anything but an error message sed: illegal option -- d on the systems I have access to.

Does it have to be sed ? How about

ls -l | awk '{print $5, $9}'

EDIT: This sed might work as well:

ls -l | sed 's/\([^ ]* *\)\{4\}\([^ ]* *\)\([^ ]* *\)\{3\}\([^ ]* *\)/\2\4/'

Thanks for your quick reply, Rudic.

So, you mean that awk+print finds the way to get the records 5 and 9 of each line? so simple... I'll try this evening (no access to shell now) Thanks!!!! :):slight_smile:

My problem with sed was about formulating the regular expression. I made something like:

sed -d '\-rw-r-\-\r-\-$'

and it deleted also the 'r' in $9...

If you have any website suggestion to learn well how to formulate regular expressions, it would be very nice... but I am already very thankful! :slight_smile:

I'd be very curious about your sed version...

With shell-builtins

ls -l | while read x x x x size x x x file; do printf "%s %s\n" "$size" "$file"; done

I also have never met a sed -d option ...
What is your OS?

uname
uname -sr