Visit The New, Modern Unix Linux Community


Vi delete line with second occurence of pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Vi delete line with second occurence of pattern
# 1  
Question Vi delete line with second occurence of pattern

I have a large file and many lines are duplicated. I'm trying to delete lines with every second occurrence of a pattern. Did tried searching similar question but no luck.


I can delete all lines matching pattern with :g/pattern/d but don't want to lose data.


Sample pattern to delete John-------Doe
Code:
Sample data:
Time--------FName---------LName
11:05-------John------------Doe
11:05-------John------------Doe
11:06-------Michael---------Lawrence
11:06-------Michale---------Lawrence

Expected result:
11:05-------John------------Doe
11:06-------Michael---------Lawrence


Last edited by rbatte1; 03-26-2015 at 01:43 PM.. Reason: Added ICODE tags
# 2  
Quote:
Originally Posted by homer4all
...
Sample pattern to delete John-------Doe
Code:
Sample data:
Time--------FName---------LName
11:05-------John------------Doe
11:05-------John------------Doe
11:06-------Michael---------Lawrence
11:06-------Michale---------Lawrence

Expected result:
11:05-------John------------Doe
11:06-------Michael---------Lawrence

Why do you expect the last line to be removed?
- It is not a duplicate since "Michale" != "Michael"
- It doesn't match the pattern either.

In any case, try this:

Code:
:%s/\v^(.*John------------Doe.*)\n\1/\1/

I have tried it in vim though. It's difficult to get a hold of vi since it's almost always a link to vim in Linux/Cygwin these days.
This User Gave Thanks to durden_tyler For This Post:
# 3  
If there is any chance that the duplicated lines won't be adjacent, awk may be a much better tool for this than vi.

In addition to the misspelling on the last input line; why shouldn't the header line appear in the output? Assuming you do want to keep the header, you could try something like:
Code:
awk 'f[$0]++ == 0' input_file > tmp.$$ && cp tmp.$$ input_file;rm tmp.$$

which, with your sample input, updates your input file to be:
Code:
Time--------FName---------LName
11:05-------John------------Doe
11:06-------Michael---------Lawrence
11:06-------Michale---------Lawrence

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Code:
uniq file > newfile

The newfile will have the adjacent duplicate lines removed.
# 5  
Don and Durdent .. thank you for those inputs and appreciate your replies, it help to solve my problem, also yes last line was misspell.. Smilie
# 6  
Just a quick note - you can run Don's awk script inside the vi/vim editor.
Essentially, you can run any command of the OS on which your vi/vim editor was installed.

Code:
:%!awk 'f[$0]++ == 0'

"%" ==> set the entire file as the range on which the awk command will run
"!" ==> run the command of the underlying OS.
"awk 'f[$0]++ == 0' ==> the actual OS command; the "input_file" isn't required here

Once you hit Enter, your duplicate data will be removed within the editor window itself. You can then simply save it. This obviates the need for the temp file.
# 7  
Quote:
Originally Posted by durden_tyler
Just a quick note - you can run Don's awk script inside the vi/vim editor.
Essentially, you can run any command of the OS on which your vi/vim editor was installed.

Code:
:%!awk 'f[$0]++ == 0'

"%" ==> set the entire file as the range on which the awk command will run
"!" ==> run the command of the underlying OS.
"awk 'f[$0]++ == 0' ==> the actual OS command; the "input_file" isn't required here

Once you hit Enter, your duplicate data will be removed within the editor window itself. You can then simply save it. This obviates the need for the temp file.
If you're in the editor to make other changes to the file anyway, this is a great idea. But, it doesn't get rid of the need for a temp file; it just has vi create the temp file instead of the user creating it explicitly. In fact, there is a good chance that vi (or vim) will create another temp file or two to process the input and output for awk.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #231
Difficulty: Easy
The TCP/IP communications protocols were developed for the ARPANET by Robert Kahn, Vint Cerf and Don Henley of the Eagles.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Delete after nth occurence of string in each line

Hello, Environment: I am under Ubuntu 18.04 bionic. I have an sql file consisting of 10K lines. Objective: What I am trying to attain is to remove everything coming after 2nd tab in each line. While searching for the answer, I found two answers and both gave expected result just for the first... (2 Replies)
Discussion started by: baris35
2 Replies

3. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. Linux

Problem editting the first occurence of a pattern in the first uncommented line

Hi I have to replace a pattern found in the first uncommented line in a file. The challenge I'm facing is there are several such similar lines but I have to edit only the first uncommented line. Eg: #this is example #/root/xyz:Old_Pattern /root/xyz:Old_Pattern /root/xyz:Old_Pattern ... (10 Replies)
Discussion started by: Stoner008
10 Replies

6. Shell Programming and Scripting

delete line upto the nth occurence of a particular charachter.

hi all i want to delete a line upto a particular character. here is example. cp cms/images/wifi-zone.png i want to delete the line till . (cp cms/images/wifi-zone.) so the output wud be "png" only how can i do it? also please note down that dot (.) can also occur multiple... (12 Replies)
Discussion started by: kashifv
12 Replies

7. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

8. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

9. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

10. Shell Programming and Scripting

Perl onliner to search the last line with an occurence of a pattern

Hi I need a perl onliner which seaches a line starting with a pattern(last occurence) and display it. similar to grep 'pattern' filename | tail -1 in UNIX Ex: I want to display the line starting with "cool" and which is a last occurence adadfadafadf adfadadf cool dfadfadfadfara... (4 Replies)
Discussion started by: ammu
4 Replies

Featured Tech Videos