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.
I am using ubuntu 8.04
What i want to do is delete those lines( those which do not contain any character OR just spaces OR start with a digit then whatever follows is irrelevant)
I have tried few things in vi like
:%s/^\d.*//g
But still that empty line remains
i also tried in terminal
$cat file | grep -iv '^0'
Still those lines that begin with number other than 0 are not removed
Last edited by sumeet inani; 01-23-2010 at 05:06 AM.
So the lesson is
(1)For filtering we can pass grep output to another grep.
(2)i had forgotten the metacharacter [0-9] meaning any number from 0 to 9(both included).
(3)-v means output those lines which do not match the pattern.
(4)'^ *$' means those lines that start with space followed by 0 or more space characters till end.
(3)-p option in cp means preserve mode,ownership,timestamp.
You are great Disillusionist.
Am I right ?
Also I was thinking that in vi editor if we replace those lines by BLANK(say) and use
:map k Ndd
because I use arrow key for navigation not hjkl.
I tried 50k after running command /BLANK and cursor located at end of file.
but it did not delete 50 BLANK lines just one that was 50th above.
Last edited by sumeet inani; 01-23-2010 at 07:41 AM.
I get it.
So I think ^ and $ just means start & end of line.We should NOT combine ^ with space and infer that line has to start with space.
I have also noticed that say if we want line to begin & end with 'a' while anything can occur in between then
/^a.*a$
here . means any character & * causes any character to occur zero or multiple times.so line 'aa' will also match.
I also came to know \_ allows multiple lines.
But I don't understand it fully ?
Also what do you suggest about the thing I tried in vi mentioned in my second post ?
Last edited by sumeet inani; 01-24-2010 at 01:50 AM.
Also what do you suggest about the thing I tried in vi mentioned in my second post ?
Ndd is two commands, N and dd. N means search backwards for the last specified search string. In the test you describe there probably was no "last specified search string" so the N had no effect. The command to delete 50 lines is 50dd.
I don't know the command to delete 50 lines matching a search string (there almost certainly is one -- vi is very powerful); 50ndd would search forwards 50 times and then delete the current line.
As I have done /BLANK then mapping then went to end of file then 50k.
You are right I think 50Ndd evaluates to search 50 times backward then delete that BLANK.
There should be way to enclose Ndd in brackets so that 50 times the whole sequence of two commands would get executed.
Somebody who knows more about vi editor can solve this query also I don't understand \_ in search fully.
To jschiwal
it also worked well in vi.
BUT
if a line contains just a tab then
g/^[[:space:]]*$/d deletes it but not g/^[ ^I]*$/d
I think the command is
:g/PatternToDelete/d
can you point me to more info about this g command ?
i also found out that
[:alnum:] Match all letters and digits.
[:blank:] Match the space and tab characters.
[:digit:] Match digits.
[:lower:] Match lowercase letters.
[:return:] Matches the end-of-line
[:space:] Match all whitespace characters.
[:tab:] Match the tab character
[:upper:] Match the uppercase letters.
i think we have to use these inside character classes ?
Last edited by sumeet inani; 01-25-2010 at 02:10 AM.
The character inside "[ ^I]" is an eye (I) and not an ell (l). A control-I to be exact. Don't enter ^ & I separately,
Entering ctrl-v TAB may depend on using bash.
I got it we have to press ctrl+i.
One thing I have noted that /^[:space:]*$ is better than /^[^I]* because latter does not match with line containing just spaces.
This link was very useful http://vimdoc.sourceforge.net/htmldoc/usr_10.html#10.1
For benefit of all users.Let me reiterate
If you want to perform an action multiple times then record a macro
q<register>
register can be a to z in which macro will be recorded.
Whatever you want to do multiple times.Do it once.
q
This will stop recording
Now you can execute that macro multiple times
<count>@<register>
Thank You.
Last edited by sumeet inani; 01-25-2010 at 12:07 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.