LinuxQuestions.org
Visit Jeremy's Blog.
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 02-01-2018, 12:33 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
Search exact string and get line number from where it is in text


Imagining that i have this text :

Quote:
ewrwrwerwetew
ttyt56hfghh
user
7547754
36346fgjfgj
I want to search for an exact string and get the line number in that text file from where it is .

Using grep for exact match i can use :

Code:
grep "\<user\>" <textfile
but how do i get the line number from where "user" word it is ?
in the text above , "user" is in line 3 , how can i get that value ?
 
Old 02-01-2018, 12:48 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,319
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
Check the manual page for grep

Code:
man grep
Scroll down to where it says:
Prefix each line of output with the 1-based line number within its input file.
 
1 members found this post helpful.
Old 02-01-2018, 01:04 PM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
it is the "-n" switch ?
so the search should be ?
Quote:
grep -n "\<user\>" <textfile
 
Old 02-01-2018, 01:12 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,319
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
Or this instead,

Code:
grep --line-number "\<user\>" textfile
Whichever style you prefer to look at.
 
2 members found this post helpful.
Old 02-01-2018, 02:48 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
thanks , it works line a charm .
 
Old 02-02-2018, 10:02 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
Another grep option -w can be used instead of the \< \>
Code:
grep -nw "user" textfile
-nw is short for -n -w
The following prints only the line number:
Code:
sed -n "/\<user\>/=" textfile
And awk can elegantly pass strings (constants and shell variables) to its own variables:
Code:
awk -v u="user" '($1==u) {print NR}' textfile
 
  


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
(SOLVED) Search and Replace Exact string without removing the line pedropt Programming 6 09-10-2017 03:53 AM
[Close] search for exact string on file and delete that line pedropt Programming 3 07-11-2017 03:36 PM
[Solved] Search & Replace exact string with sed pedropt Programming 17 06-25-2017 02:57 PM
Exact string match in multi line string Arun Shankar Programming 2 06-19-2014 09:54 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM

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

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