• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

regexp help for vi, grep, or sed

TeMpT

Senior member
Anyone know how you go about quantifying the end of line or carriage return mark in regexp?
In vi, I am trying to do a global replace of characters (xxx, for example) with a line carriage and don't know what to put in the replacement part....so far, I have:

:g/xxx/s//LINE_CARRIAGE/g

anyone?

Want to use this in my grep too....so that I can get rid of all the blank lines
 
A basic regular expression doe not span lines. Sed should use \n as an extension to a basic regular epression to allow you to do this.

If you want to 'grep' over a new line, you may be able to do this with egrep, but I'm nt certain. grep will not do this.
 
This is not highly technical. You could get this from any regex manual.

But since no one has answered your question:

I do not know of a way for vi to match multiple lines. You can use sed, awk, or perl.

Sed:

The uppercase versions of the commands are for multi-line mode. D deletes the first line, P prints the first line, etc.

sed '/^foobar$/ {N; s/^foobar\nfoo/bar/;}'

Awk:

See the getline and next commands. You can also store the previous line in a variable.

Perl:

You can store the previous line in a variable, or you could do something like the following:

my @array = <IN>;
# Joins each line with a : separating them.
# You probably would want to use something other than a colon and actually check to make sure that (control?) character
# does not exist in your input, I'm just providing an example.
my $longline = join ':', @array;

Then do whatever matches you want on $longline. This is pretty bad code, but I don't know what in particular you are trying to do so I can't offer better.
 
Well for a simple find characters and replace them with a carriage return in vi the following will work. For example if I wanted to replace the word "example" in the middle of that with a carriage return:

This is an example of replacing.

Then the following vi command would do that:
:g/example/s//Ctrl-VCtrl-M/g

Now that is exactly what it looks like, vi uses a Ctrl-V character to precede other Ctrl characters in it's regular expressions, so you type Ctrl-V, and then Ctrl-M. This will replace every occurrence of example with a carriage return.

In response to moohooya's:


<< If you want to 'grep' over a new line, you may be able to do this with egrep, but I'm nt certain. grep will not do this. >>



In most un*x regexp's '^' denotes the beginning of a line, and '$' denotes the end of a line, keeping that in mind then the following will do your remove empty lines in grep...

cat file | grep -v "^$"

and see how many carriage returns you see! That will strip out all blank lines, like the original poster wanted I believe.
 


<< 'XXX$^' >>



That is not going to work. For the general form in multi-line regex's ^ means the beginning of the regex and not the beginning of the line. $ is the end of the regex, and not the end of the line. If you read in two lines ^ is the beginning of the first line, and $ is the end of the second. Now when in single-line mode, the ends are the line so ^ is the beginning of the line and $ is the end of the line.

I have heard that you can do multi-line regex's in vi if you use the keystroke recorder, match the first line and play back the keystrokes to match the second and perform the action. It seems like a big hack to me and probably more work than its worth.
 
Hmmm....are you just replacing a single character with a newline? Cause if you are just use "tr".

tr '\015' '\012' <input filename> <output filename>

The above will replace all Windows and Mac style carrage returns (ASCI # 015) with Unix style carrage returns (ASCI # 012). Not sure if you need the ASCI number of the character if its a normal character that you can type, but since these two are not, you have to use the ASCI number.
 
Back
Top