Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
(the spaces at the beginning of the line are included, with the exception of "108 saaafiles").
What I'm trying to do is first to delete the digits at the beginning of the line. So it should work for '108 saaafiles', but it doesn't. It shows the same output. Here's what I've tried:
Code:
ls -s | sed -e '1d' -e '/^[[:digit:]]*/d'
And neither does /^[[:space:]]*/d' work in order to delete all spaces at the beginning of the line for the other files.
In sed the d command deletes a line. 1d deletes the first line. /^[[:digit:]]*/d deletes all lines that start with zero or more digits (=> all lines!).
I guess you want to delete characters, to be done with the s command (substitute):
Code:
sed -e 's/^[[:digit:]]*//'
Substitute the leading digits by nothing.
BTW if your goal is to delete the first column, then consider a simple
Code:
ls
Last edited by MadeInGermany; 11-23-2016 at 08:10 AM.
So, I am curious, why would you use the '-s' option to make ls output the size of each file and then, use sed to get rid of the one thing you told ls to give you??
Well, I'm doing it for the sake of exercising. I'm not interested in being smart by changing from ls -s to ls or simply ls -l, I'm interested in being able to manipulate a certain type of output which could exist in a different context where there's nothing else I could.
Indeed, later I realised that /d deletes the whole line, so I too turned to 's/pattern//'.
@madeingermany so you're saying that /^[[:digit:]]*/d deletes all lines that start with zero or more digits. Shouldn't the same be applied to sed -e 's/^[[:digit:]]*//'? Or rather, how does that translate when you're using substitution, instead of deletion? As far as I can see, it does delete the digits at the beginning of the line. So it will only delete those digits and nothing else, right, even though [[:digit:]]* means 0 or an indefinite number of digits. I understand now the behaviour, but it seems somewhat contradictory when changed to substitution.
How come sed -e 's/^[[:space:][:digit:]]*//' works and only the name of the files themselves are shown? In most files there're 2 spaces at the beginning of the line, then a number, and then again a space! So how does this expression match all those three? Does it simply substitute all spaces and digits it encounters at the beginning of the line? That was, after all, the expression I was looking for.
You have to break it down into its parts. The outer square backets [ ] mean the set of anything within. In this case the set is anything that is a whitespace and anything that is a digit:
Code:
sed -e 's/^[[:space:][:digit:]]*//'
So it's getting a span of whitespace and/or digits starting with the beginning of the line and ending when encountering the first character that is neither a whitespace or a digit. Then when (if) it has that, it is replaced and the next line is read.
Edit: See man 7 regex
Last edited by Turbocapitalist; 11-23-2016 at 12:10 PM.
You have to break it down into its parts. The outer square backets [ ] mean the set of anything within. In this case the set is anything that is a whitespace and anything that is a digit:
Code:
sed -e 's/^[[:space:][:digit:]]*//'
So it's getting a span of whitespace and/or digits starting with the beginning of the line and ending when encountering the first character that is neither a whitespace or a digit. Then when (if) it has that, it is replaced and the next line is read.
Great. And now I added [:alpha:] and it outputs only the extensions, because it stops at punctuation. Thanks!
Well, I'm doing it for the sake of exercising. I'm not interested in being smart by changing from ls -s to ls or simply ls -l, I'm interested in being able to manipulate a certain type of output which could exist in a different context where there's nothing else I could.
You might want hit up the manual pages, faqs, howtos and even online tutorials that will give you many, many, many use cases and explanations. There are really great online tutorials for sed/awk/grep now.
Last edited by szboardstretcher; 11-23-2016 at 12:13 PM.
You might want hit up the manual pages, faqs, howtos and even online tutorials that will give you many, many, many use cases and explanations. There are really great online tutorials for sed/awk/grep now.
That's what I'm currently doing already. It's part of an exercise from lynda But I need concrete examples and sometimes people who can add their knowledge and come up with all sorts alternative solutions and so on.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.