Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Vi delete line with second occurence of pattern Post 302939780 by Don Cragun on Saturday 28th of March 2015 10:53:12 PM
Old 03-28-2015
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
VITMP(1)						      General Commands Manual							  VITMP(1)

NAME
vitmp - edit temporary files SYNOPSIS
vitmp [VIM options] FILE... DESCRIPTION
vitmp is a wrapper around the VIM editor which may be used to invoke the editor in a way that is guaranteed to be suitable for editing tem- porary files used with programs such as crontab(1) and edquota(8). The instance of the editor that is invoked is /bin/vi. In particular, it is guaranteed that the file, if edited, will be overwritten in place such that the modified contents may be read back via a previously obtained file descriptor. Invoking the editor via vitmp also makes sure that no additional editor temporary files are used. Please note that vitmp offers no crash recovery capabilities. If the editor crashes during the overwrite, data may be lost. HISTORY
vitmp first appeared in Openwall GNU/*/Linux 1.0. AUTHORS
Solar Designer <solar@owl.openwall.com> SEE ALSO
crontab(1), vi(1), vim(1), edquota(8) Openwall Project 25 April 2002 VITMP(1)
All times are GMT -4. The time now is 08:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy