PDA

View Full Version : Need help in Vim editor



oakdeveloper
July 30th, 2009, 06:59 PM
Hi,

I have a text file which has 20 lines. In all the lines, the beginning of the line has a 3 digit number followed by a space. I tried to do a search and replace in Vim to remove those 3 digits but it doesn't seem to work. Please help me..


:%s/\d{3}//gcThis is the command I gave. But it says "Pattern not found"

Thanks,
Babu

arvevans
July 30th, 2009, 07:08 PM
Pattern matching can be difficult until one has lots of experience in doing it.

As an alternative, and simpler solution...why not save the file and then use the "cut" command to mask off those first 3 character positions:

echo filename | cut -c 4- > newfilename

This might be faster and is based on positional parameters rather than pattern matching.

Arv
_._

Dill
July 30th, 2009, 07:09 PM
I believe you need to precede the number of matches with another backslash:


:%s/\d\{3}//gc

Try that and see if it works.

Cheers,
Dill

oakdeveloper
July 30th, 2009, 07:13 PM
Thanks a lot! It works.

dandaman0061
July 30th, 2009, 07:17 PM
Yet another suggestion...
Don't get me wrong, regexes definitely have their place, and depending on the size/number of lines in the file, it may be the way to go. Anyway here is how I would do it (and not have to worry about regex syntax)

- Move cursor to very beginning of file (keystroke: gg)
- Go into visual mode (keystroke: Ctrl+v) notice lowercase 'v'
- Move cursor to bottom of file (keystroke: G)
The whole first column should be highlighted now
- Move cursor to the right 2 places, highlighting the 3 columns (keystroke: ll)
- Now delete them (keystroke: x)

Yay vim.

oakdeveloper
July 30th, 2009, 07:23 PM
Wow! Works like a charm!!! Thanks dandaman0061 (http://ubuntuforums.org/member.php?u=126481)

Greyed
August 2nd, 2009, 11:42 AM
Also when trying to define the pattern try turning on incsearch and then constructing the pattern with a plain / first. This way you get some sort of feedback as to how far the pattern is matching and when it stops matching. In this case you'd type in /\d and it would match the first digit but as soon as you typed the non-escaped { the incsearch would fail to match. You'd know that's your problem spot. ;)

veda87
August 2nd, 2009, 07:33 PM
try this too... to remove first 3 chars from all the lines

:1,$s/^...//
number of dots indicates number of character to be removed.....

Keith_Beef
August 31st, 2009, 08:04 PM
Hi,

I have a text file which has 20 lines. In all the lines, the beginning of the line has a 3 digit number followed by a space. I tried to do a search and replace in Vim to remove those 3 digits but it doesn't seem to work. Please help me..


:%s/\d{3}//gcThis is the command I gave. But it says "Pattern not found"

Thanks,
Babu

Here's yet another way to delete three digit numbers at the start of lines:

:1,$s/^[0-9]\{3\}//

K.

petrus4
August 31st, 2009, 08:23 PM
Hi,

I have a text file which has 20 lines. In all the lines, the beginning of the line has a 3 digit number followed by a space. I tried to do a search and replace in Vim to remove those 3 digits but it doesn't seem to work. Please help me..


:%s/\d{3}//gcThis is the command I gave. But it says "Pattern not found"

I don't know how to do that as a vim command, but I'd quit vim momentarily and write a shell script, like so:-


#!/bin/sh

for i in `cat file` # whatever the file is called
do
newstring=`echo $i | sed s"@^[0-9][0-9][0-9] @@"g`
echo ${newstring} >> file.bak
done

mv file.bak file

Make sure you use two > signs in the above, as well; that will tell echo to add the next string to the file, rather than replacing everything else in the file with the new string.

DaithiF
August 31st, 2009, 09:34 PM
just for fun, a few more ways in vim

:%normal 3x
similar to some earlier suggestions except that %s is easier than 1,$s

:%s/^[0-9]\{3\}//
:%!cut -c4-
:%!sed 's/^[0-9]\{3\}//'

Gen2ly
August 31st, 2009, 10:16 PM
...

similar to some earlier suggestions except that %s is easier than 1,$s


:%s/^[0-9]\{3\}//
:%!cut -c4-
:%!sed 's/^[0-9]\{3\}//'


Good tips. Vim's freakin' cool. Forgot all about executing a command. I've tried \d before with sed and it doesn't look like it supports it. What does the 1,$s do? I've seen this before but usually only on older vim/sed(?) examples. Obviously 's' is for substitute but what do the other characters mean and why would one use it?

DaithiF
August 31st, 2009, 11:11 PM
1,$ means apply the action that follows ('s' = substitute), on the range of the file from line 1 to $, where $ means the end of the file.
%s means apply the command 's' globally, ie, to every line, and so its equivalent (but shorter) than 1,$s

Gen2ly
September 1st, 2009, 12:35 AM
Ah cool (only known global to this point). Haven't needed to to ranges yet. Suppose that's next. :)