Subject: | |
From: | |
Reply To: | |
Date: | Fri, 29 Apr 2016 09:54:47 -0700 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
On 4/29/16 8:23 AM, krikor Gullekian wrote:
> How can I delete in an easy way a line if the first 30 characters are
> blank in unix. I use sed, "sed '/^$/d'" but this will delete only if
>the entire line is blank.
Regular expressions are your friend. Observe:
fuji:~ root# cat demo.txt
zero
one
two
three
four
five
six
seven
eight
nine
ten
fuji:~ root#
So we have a file in which each line starts with one more space than the
line before. If we want to delete all lines beginning with at least one
space...
fuji:~ root# sed '/^ /d' demo.txt
zero
fuji:~ root#
Now delete all lines beginning with at least three spaces...
fuji:~ root# sed '/^ /d' demo.txt
zero
one
two
fuji:~ root#
Finally all lines beginning with seven spaces...
fuji:~ root# sed '/^ /d' demo.txt
zero
one
two
three
four
five
six
fuji:~ root#
So, obviously, you could accomplish your task in brute force fashion by
typing out 30 spaces in the sed pattern, rather than 1, 3 or 7 as I have
shown. But better would be to use a regular expression similar to this:
fuji:~ root# sed '/^ \{9\}/d' demo.txt
zero
one
two
three
four
five
six
seven
eight
fuji:~ root#
Here we're saying: "delete all lines beginning with some pattern, which
happens to be a space, in which that pattern is repeated 9 times". Note
that the curly braces {} have to be escaped in order to give them that
special meaning, otherwise you'd be matching the literal curly brace
characters. So in your case, of course, you'd use 30 instead of 9.
Regards,
Barry Lake
Allegro Consultants, Inc.
* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *
|
|
|