LinuxQuestions.org
Review your favorite Linux distribution.
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 01-23-2010, 03:59 AM   #1
sumeet inani
Member
 
Registered: Oct 2008
Posts: 901
Blog Entries: 26

Rep: Reputation: 49
trying to do task in vi editor


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 04:06 AM.
 
Old 01-23-2010, 04:12 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
Code:
cp -p file file.bak
grep -v '^ *$' file.bak|grep -v '^[0-9]' > file
Ensure the new file contains what you wanted before removing file.bak

Last edited by Disillusionist; 01-23-2010 at 04:21 AM. Reason: More efficient with just two greps
 
1 members found this post helpful.
Old 01-23-2010, 06:27 AM   #3
sumeet inani
Member
 
Registered: Oct 2008
Posts: 901
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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 06:41 AM.
 
Old 01-23-2010, 08:02 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
Quote:
Originally Posted by sumeet inani View Post
So the lesson is
(4)'^ *$' means those lines that start with space followed by 0 or more space characters till end.
Am I right ?
Not quite:

'^' matches the start of the line
' *' matches any number of space characters (including zero)
'$' matches the end of the line

Therefore '^ *$' matches either a blank line, or a line containing nothing but space characters.
 
Old 01-24-2010, 12:36 AM   #5
sumeet inani
Member
 
Registered: Oct 2008
Posts: 901
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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 12:50 AM.
 
Old 01-24-2010, 01:20 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by sumeet inani View Post
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.
 
Old 01-24-2010, 02:33 AM   #7
sumeet inani
Member
 
Registered: Oct 2008
Posts: 901
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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.
 
Old 01-24-2010, 02:40 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
You can use
:g/^$/d
to delete all blank lines in the file using vim.

To include lines consisting of spaces and/or tabs:
:g/^[ ^I]*$/d
or
:g/^[[:space:]]*$/d

In vim or the shell you can enter TAB by pressing CTL-V TAB. In vim you can also simply hit the TAB key.

Last edited by jschiwal; 01-24-2010 at 02:50 AM.
 
1 members found this post helpful.
Old 01-24-2010, 03:27 AM   #9
sumeet inani
Member
 
Registered: Oct 2008
Posts: 901
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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 01:10 AM.
 
Old 01-24-2010, 04:47 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
 
Old 01-24-2010, 06:27 AM   #11
sumeet inani
Member
 
Registered: Oct 2008
Posts: 901
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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.
 
Old 01-24-2010, 05:28 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
For advanced repetitions, look in the vim manual for macros : http://vimdoc.sourceforge.net/htmldoc/usr_10.html#10.1
 
1 members found this post helpful.
Old 01-24-2010, 11:04 PM   #13
sumeet inani
Member
 
Registered: Oct 2008
Posts: 901
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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-24-2010 at 11:07 PM.
 
Old 01-24-2010, 11:08 PM   #14
sumeet inani
Member
 
Registered: Oct 2008
Posts: 901
Blog Entries: 26

Original Poster
Rep: Reputation: 49
QuestionOne thing remains.That is I want to understand usage of \_ in searching in vi.
 
Old 01-24-2010, 11:28 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
Never seen that (\_), I basically use these methods: http://www.felixgers.de/teaching/ema...h_replace.html
 
  


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
kernel panic attempted to kill the idle task! in idle task - not syncing dudutworld Linux - Newbie 2 09-16-2013 01:50 PM
Task bar no longer shows task Richard Rahl Linux - Newbie 3 04-12-2010 09:39 AM
linux distro / photo editor / music editor of choice ?? expatcanuck Linux - Newbie 1 05-04-2009 05:24 PM
LXer: Open Movie Editor: Linux Video Editor with Plot Twists LXer Syndicated Linux News 0 10-27-2008 09:50 PM
A little task brazilian_user Linux - General 1 08-28-2006 01:34 PM

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

All times are GMT -5. The time now is 09:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration