LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-11-2009, 11:29 PM   #1
d3funct
Member
 
Registered: Jun 2001
Location: Centralia, WA
Posts: 274

Rep: Reputation: 31
Question I need a vi regex


I'm a SAN admin, and I'm currently upgrading a *hit load of switches. Before I do the upgrades I need to add some information from a data collection into a spreadsheet.

Here is an example line of data:

0 (00) G_Port {hostname-#} true 16 0

I want to delete everything before G_Port (can be G, E or F_PORT) and after the 'hostname-#' (hostname and # vary). I'm using vi to edit the data collection, and the already exists, so I can't just make a .csv and create a new spreadsheet. I'm copying the *_Port and pasting it into one column, and the same with the hostname into another column. I have 143 ports in each file and have about 30 files to go through. I'm getting tired of going line by line and doing "d4w" 143 times and then going back through and deleting end of line after the hostname to get the two entries I want to keep. Is there a regular expression I can use in vi that will do this. I'm thinking something similar to "1,$s/yada/yafa/g" only instead of using the substitute flag, use something to delete the first four "words" 0 (00) and then delete everything after "hostname"?

Any suggestions are greatly appreciated.
 
Old 03-12-2009, 03:40 AM   #2
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
I don't know vi but how about

grep -o '[GEF]_PORT {a-zA-Z0-9-]}'
 
Old 03-16-2009, 09:11 AM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Or:
Code:
grep -o '[GEF]_PORT .*\}'
Sorry, I only know enough vi for emergency use.
 
Old 03-16-2009, 09:26 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
sed 's/.*\([GEF]_Port {.*}\).*$/\1/' testfile
If the output looks OK, you can add the -i for in place editing and don't need to redirect to a second file.

Vim is based on ed regular expressions, as is sed:
:%s/.*\([GEF]_Port {.*}\).*$/\1/

So the only difference is the line range which precedes the command. The % means to search & replace on every line.
You could modify the current line and the next 9 lines with
:.,+9s/.*\([GEF]_Port {.*}\).*$/\1/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with regex tbeehler Linux - Software 4 07-11-2008 10:05 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Regex help Penguin of Wonder Programming 4 08-06-2007 07:04 AM
regex help siyisoy Programming 4 04-07-2006 05:32 AM
Regex Help cmfarley19 Programming 5 03-31-2005 10:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration