Get a specific line number from a text file | Unix Linux Forums | Linux

  Go Back    


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

Get a specific line number from a text file

Linux


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-27-2010
barryxian barryxian is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 29 July 2010, 3:36 AM EDT
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Bug Get a specific line number from a text file

Hello! All,
Could you please tell me how to get a specific line number from a text file?

For example below,

ABC
DEF ---> Get this line number, return to an variable
GHI

My OS is Linux.

Thank you so much for your help in advance!
Sponsored Links
    #2  
Old 07-27-2010
bankai bankai is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 18 July 2011, 6:12 PM EDT
Posts: 51
Thanks: 0
Thanked 7 Times in 7 Posts
VAR=`grep -n "text" <file>`

Last edited by bankai; 07-27-2010 at 11:47 PM..
The Following User Says Thank You to bankai For This Useful Post:
barryxian (07-28-2010)
Sponsored Links
    #3  
Old 07-27-2010
kurumi kurumi is offline
Resident Ruby Wrangler
 
Join Date: Dec 2009
Last Activity: 12 January 2014, 4:32 AM EST
Posts: 601
Thanks: 2
Thanked 83 Times in 78 Posts

Code:
#!/bin/bash
i=0
while read -r LINE
do
  ((i++))
  case "$LINE" in
   "DEF")  echo "line num: $i";;
  esac
done <"file"



---------- Post updated at 10:43 PM ---------- Previous update was at 10:42 PM ----------


Code:
grep -n "DEF" file



---------- Post updated at 10:44 PM ---------- Previous update was at 10:43 PM ----------


Code:
sed -n '/DEF/=' file #GNU

The Following User Says Thank You to kurumi For This Useful Post:
barryxian (07-28-2010)
    #4  
Old 07-28-2010
barryxian barryxian is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 29 July 2010, 3:36 AM EDT
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Hi! bankai & kurumi,
Thanks for your reply, yes, "grep -n" is good.

I am sorry for my unclear question.
Actually here is situation...
DEF is a procedure and will be called for several times.....
What I am going to do is,
I would like to add codings into "that file", and if any result from DEF is wrong, it will display that line number....

For example,
if the result from 2nd DEF is wrong, I will display that line number.....
which is line#5.

ABC
DEF --> 1st DEF
LLL
KKK
DEF --> 2nd DEF
PPP
DEF --> 3rd DEF


Also, in a test file, if we right click "View" and select "What Line Number?" then a line number will display.
Can we do this?

Thanks a lot!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inputting text to a specific line of a file JayC89 UNIX for Dummies Questions & Answers 6 07-02-2010 03:21 AM
extract the lines between specific line number from a text file return_user Shell Programming and Scripting 1 07-18-2009 09:11 PM
Modify Specific Line of a Text File msb65 Shell Programming and Scripting 5 02-12-2009 04:10 PM
Adding specific text and spaces to each line in a text file hertingm Shell Programming and Scripting 4 08-25-2008 02:34 PM
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 08:29 AM



All times are GMT -4. The time now is 03:14 PM.