Need pattern matching in vi editor


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Need pattern matching in vi editor
# 1  
Old 01-17-2011
Lightbulb Need pattern matching in vi editor

Hi,

I want to remove all the digits/characters after the last underscore in the following set of lines

Code:
abc_cdef_12_456_98765
defs_qw_4567_23_0877_89976769870
deffon_asdcdc_23_980980_098
sdfre_rtt_grt

the result im expecting is
Code:
abc_cdef_12_456
defs_qw_4567_23_0877
deffon_asdcdc_23
sdfre_rtt

I want to know inside VI editor what pattern replacement can be used to perform the above operation.

Pl. note no.of underscores are different. After underscore any thing can come, say , character or digit!Smilie

Thanks in advance!!!

Appu

Last edited by Scott; 01-17-2011 at 07:07 AM.. Reason: Code tags, please...
# 2  
Old 01-17-2011
Code:
:,%s/_[^_]*$//

This User Gave Thanks to Scott For This Post:
# 3  
Old 01-17-2011
you could use a macro if your version of vi supports them - i've been using vim so long I can no longer remember what the original vi can do.

the macro could be:
Code:
$F_d$j

$ - go to end of line
F_ go back to the first occurance of a _
d$ - delete to the end of line
j move down one line

which can be run over the whole file by going to the top of the file and 999@a (or whatever register you save the macro to, and also change the number of times to run to match the number of lines (or greater) in the file.

Last edited by wempy; 01-17-2011 at 06:57 AM.. Reason: clarification of macro command
This User Gave Thanks to wempy For This Post:
# 4  
Old 01-24-2011
Thanks a lot guys!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

Help to find two pattern in vi editor

I want to find two pattern in vi editor.for example in file demo.txt I want to find a record such as "BDBO" and "sh". how can i find such a record bu using single command in demo.txt in vi editor. please help. ---------- Post updated at 01:49 AM ---------- Previous update was at 01:46 AM... (5 Replies)
Discussion started by: ashfaque
5 Replies

5. Shell Programming and Scripting

Vi editor deleting lines with specific pattern

Hi, I need to delete all lines in the file using vi editor which start with word aternqaco. Please assist. aternqaco.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment aternqa.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

6. UNIX for Dummies Questions & Answers

Find pattern suffix matching pattern

Hi, I am trying to get a result out of this but fails please help. Have two files /tmp/1 & /tmp/hosts. /tmp/1 IP=123.456.789.01 WAS_HOSTNAME=abcdefgh.was.tb.dsdc /tmp/hosts 123.456.789.01 I want this result in /tmp/hosts if hostname is already there dont want duplicate entry. ... (5 Replies)
Discussion started by: rajeshwebspere
5 Replies

7. Shell Programming and Scripting

Find and replace pattern in VI editor

All, I have a text file which has the following data X_SQL_13,X_SQL_14,X_SQL_15,X_SQL_16,X_SQL_17,X_SQL_18,X_SQL_19,X_SQL_20,X_SQL_21,X_SQL_22,X_SQL_23,X_SQL_24,X_SQL_25,X_SQL_26,X_SQL_27,... (4 Replies)
Discussion started by: thana
4 Replies

8. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. 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
Login or Register to Ask a Question