LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2012, 02:04 AM   #1
tinkulim
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Rep: Reputation: Disabled
pattern match number


is there a way to number the matched pattern using sed?
 
Old 06-14-2012, 02:25 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
what do you mean by that? there are pattern groups and those are numbered, but you do not need to use grouping.
 
Old 06-14-2012, 02:51 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by tinkulim View Post
is there a way to number the matched pattern using sed?
If you want the line number the hit occurs on and the line itself, then this should work:
Code:
sed -n '/foo/{=;p}' infile
if you need to show the number of hits and the line itself you need to use something else then sed. Here's an awk solution:
Code:
awk 'BEGIN{ cntr = 1 } /foo/ { print cntr, $0 ; cntr++ }' infile
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP + MYSQL: Invalid parameter number: number of bound variables does not match... OrangeGrover Linux - Software 6 05-08-2013 05:36 PM
[SOLVED] Changing pattern using sed [number] to number. xpto09 Linux - General 2 06-22-2011 03:32 PM
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 12:36 AM
why sector number not match block number? bitzsk Linux - Kernel 1 06-09-2009 05:32 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 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