LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-03-2005, 04:13 AM   #1
orgazmo
Member
 
Registered: May 2005
Location: Paris, france
Posts: 53

Rep: Reputation: 15
Read a line in a txt file with bash


[already posted in newbie section]

Hi all!

First excuse me for my bad english, but i'm french.

Well... let's stick to the question. In fact I'm looking for a unix command that would allow me to display a precise line of a text file in the console in order to do a grep on an other file to check if the line is in the second file too.

I want to use that in a shell script.

I first planed to use diff to compare my files, but it does'nt match my needs.

Something like that would be perfect:

Command -line_number file.txt.

Thank you ;-)
 
Old 05-03-2005, 04:44 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
You could do this:
Code:
head -n $linenumber $filename | tail -n 1
 
Old 05-03-2005, 04:57 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Code:
grep -Fxf <(sed -n "$LineNumber p" /path/to/file1) /path/to/file2
Yves.
 
Old 05-03-2005, 05:15 AM   #4
orgazmo
Member
 
Registered: May 2005
Location: Paris, france
Posts: 53

Original Poster
Rep: Reputation: 15
Someone else told me to use sed in that case and actually it works well

Thank you all for your answers
 
Old 05-03-2005, 05:26 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Quote:
Originally posted by orgazmo Someone else told me to use sed...
Not here, though. That's the main reason why double- (or more) posting is forbiden by the forum rules.

Glad for you that it works, though. As for the grep part, that's for checking for the presence of the selected line(s) (you may select lines differently if you wish) in the second file, but only if the full (-x) line matches, and in "plain text" (-F), not as a regular expression.

Yves.
 
Old 05-03-2005, 07:10 AM   #6
orgazmo
Member
 
Registered: May 2005
Location: Paris, france
Posts: 53

Original Poster
Rep: Reputation: 15
I'm sorry... I didn't saw this forum first so i posted in the newbie section.

Since no one answered to me (ok that's right i did'nt wait long) i tried here, because the problem was a little about coding.

I apologize, and one more time, thanks a lot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 12:06 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 09:24 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 11:10 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration