Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
11-23-2016, 07:11 AM
|
#1
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Rep:
|
sed expression confusion delete first digit
ls -s | sed -e '1d' shows:
Code:
0 fisiera.c
0 fisiera.c.c
0 fisierb.c
0 fisierc.c
0 fisierd.c
0 fisiere.c
0 fisierf.c
0 fisierg.c
0 fisierh.c
108 saaafiles
4 sed_chall.sh
4 sed_fline.sh
(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.
Any ideas?
|
|
|
11-23-2016, 08:09 AM
|
#2
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,915
|
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
Last edited by MadeInGermany; 11-23-2016 at 08:10 AM.
|
|
1 members found this post helpful.
|
11-23-2016, 08:12 AM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,489
|
The /d means delete the whole line. What is needed instead is the substitution command.
Code:
sed -e 's/^[[:space:][:digit:]]*//'
|
|
1 members found this post helpful.
|
11-23-2016, 08:54 AM
|
#4
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Fastest way to get the needed output is:
|
|
1 members found this post helpful.
|
11-23-2016, 10:20 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
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??
|
|
1 members found this post helpful.
|
11-23-2016, 11:58 AM
|
#6
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
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.
|
|
|
11-23-2016, 12:02 PM
|
#7
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
@Turbocapitalist
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.
|
|
|
11-23-2016, 12:08 PM
|
#8
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,489
|
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.
|
|
1 members found this post helpful.
|
11-23-2016, 12:10 PM
|
#9
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
Quote:
Originally Posted by Turbocapitalist
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!
|
|
|
11-23-2016, 12:12 PM
|
#10
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Quote:
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.
|
|
|
11-23-2016, 12:14 PM
|
#11
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
Quote:
Originally Posted by szboardstretcher
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.
|
|
|
All times are GMT -5. The time now is 09:34 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|