LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 11-23-2016, 07:11 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
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?
 
Old 11-23-2016, 08:09 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,915

Rep: Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236
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.
 
1 members found this post helpful.
Old 11-23-2016, 08:12 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,489
Blog Entries: 3

Rep: Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812
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.
Old 11-23-2016, 08:54 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Fastest way to get the needed output is:

Code:
ls -1
 
1 members found this post helpful.
Old 11-23-2016, 10:20 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196
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.
Old 11-23-2016, 11:58 AM   #6
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.
 
Old 11-23-2016, 12:02 PM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
@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.
 
Old 11-23-2016, 12:08 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,489
Blog Entries: 3

Rep: Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812Reputation: 3812
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.
Old 11-23-2016, 12:10 PM   #9
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by Turbocapitalist View Post
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!
 
Old 11-23-2016, 12:12 PM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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.
 
Old 11-23-2016, 12:14 PM   #11
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by szboardstretcher View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
[SOLVED] How to use sed command to delete lines only 1-3 digit numbers on a particular column? kahthiam Linux - Newbie 4 12-27-2011 07:41 AM
What's the difference between \d , [:digit:], and [0-9] in regular expression ? 915086731 Programming 17 09-01-2011 03:56 AM
Regular expression to Grep for a n digit number somupl86 Linux - General 7 11-24-2010 06:11 AM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration