Vi remove line range containing a string


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Vi remove line range containing a string
# 1  
Old 09-07-2015
Vi remove line range containing a string

In vi I would like to remove a line containing a string. I thought after reading this I could do this.

https://www.unix.com/302297288-post3.html

Code:
:'3560,3572/gcc/d'

It keeps complaining vi mark not set. And sometimes it complains E488: Trailing characters.

I don't understand what mark not set has to do with this when I give it a range. I don't know why it thinks I have trailing characters when I don't.
# 2  
Old 09-07-2015
The single quotes are used to reference marks set with the k command. To delete lines with line numbers 3560 through 3572 (inclusive) that contain the string gcc, use the vi command:
Code:
:3560,3572/gcc/d

The post you referenced tried to use quotes instead of CODE tags to delimit the code to type into vi, but the colon should have been inside the quotes instead of outside (and using quotes instead of CODE tags confused you). I hope this explanation clears things up for you.
# 3  
Old 09-07-2015
Quote:
Originally Posted by Don Cragun
The single quotes are used to reference marks set with the k command. To delete lines with line numbers 3560 through 3572 (inclusive) that contain the string gcc, use the vi command:
Code:
:3560,3572/gcc/d

The post you referenced tried to use quotes instead of CODE tags to delimit the code to type into vi, but the colon should have been inside the quotes instead of outside (and using quotes instead of CODE tags confused you). I hope this explanation clears things up for you.
Yes that does Smilie.

Funny question though after I run it. For some reason it says 16 fewer lines
after I run the command. How is that possible? There are only 12 lines in that range. Also just for the heck of it I counted that there were only 6 lines that contained gcc. How is that possible?
# 4  
Old 09-07-2015
I apologize. I was looking at the single quotes and didn't look closely enough at the remaining command. The command you wanted is:
Code:
:3560,3572g/gcc/d

I haven't worked out what the command I gave before does...

The command:
Code:
:3560,3572d

deletes lines 3560 through 2572 (which is 13 lines; not 12).

The command:
Code:
:3560,/gcc/d

deletes from line 3560 through the 1st line after that that contains the string gcc.
# 5  
Old 09-07-2015
When I do this command I get the message below. I don't understand why it does that when 3622 is less than 3643.

Code:
:3622,3643/gcc/d

Backwards range given, OK to swap (y/n)?

Does this have anything to do with it? Do I need to include + and -?

What is the best way in Vim to operate on relative ranges in visual mode? - Stack Overflow
# 6  
Old 09-07-2015
No you need to use the g command as I showed in my last post. The command:
Code:
:g/gcc/d

will delete all lines in your file that contain the string gcc. And, the command:
Code:
:3560,3572g/gcc/d

will delete all lines in the range 3560 through 3572 that contain the string gcc.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove every line with specific string, and also the one above and below it

I would like to identify every line with a specific string, in this case: "Mamma". I would like to remove that line, and also the line above it and below it. So the below Where are all amazing Flats Look At The Great Big White Hey There Hot Mamma You Are So hot Baby I wish You were Mine... (5 Replies)
Discussion started by: phpchick
5 Replies

2. Shell Programming and Scripting

Remove last string from last line in a file

Hi, I have a file like : I want to remove last string in last line (here total string is "05550"~). And last line end with ~ character. Output should be : Please help me Thanks in advance (3 Replies)
Discussion started by: mnmonu
3 Replies

3. Shell Programming and Scripting

Remove last string from each line

I am trying to write a script that will allow me to recursively look at my directories, and output all filenames to a txt document. I am almost finished, however I am hitting one last snag. Here is my script so far: find . | grep .jpg | awk -F"/" '{print $NF}' > output.txtThis will give me an... (7 Replies)
Discussion started by: Davinator
7 Replies

4. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

5. Shell Programming and Scripting

remove unwanted specific line range

Hello everyone...I have large txt file and I would like to remove unwanted specific line. My data is like this: So I would like to remove from line below No. until line reassambled like this: Thanks... (4 Replies)
Discussion started by: taxi
4 Replies

6. Shell Programming and Scripting

Grep a string in a range and delete the line

Hi, i need to delete a lines after searching a particular string but this searching should only happen after the 4th line.. basically imagine a file like this From: abcd.yahoo.com To: cdeb.yahoo.com Subject: hi all sdfsd sadasd asdasd dfsdf From: abcd.yahoo.com To:... (3 Replies)
Discussion started by: depakjan
3 Replies

7. Shell Programming and Scripting

How to remove new line char from a string

Hi Can anyone tell me how can i remove new line character from a string. My requirement is to read a line from a file and store it to a string. read line string1=$line read line string2=$line echo $string1$string2 The result i am getting in different line. i want the output in the same... (1 Reply)
Discussion started by: sreedivia
1 Replies

8. Shell Programming and Scripting

Replace string in a file within a range of line

Hi, I want to replace the srting '; with ABCD'; in a file from line 1 to line 65. Is there any single command to do it without using awk Thanks for quick reply https://www.unix.com/images/misc/progress.gif (3 Replies)
Discussion started by: tosattam
3 Replies

9. Shell Programming and Scripting

Remove Line that contains specific string

I am looking for a way to remove any line in a text file that contains the string "Mac address". I guess you would grep and sed, but I am not sure how to do this. Thanks for you help. (3 Replies)
Discussion started by: CBarraford
3 Replies
Login or Register to Ask a Question