LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-13-2012, 07:06 PM   #1
jgrizich
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Rep: Reputation: 0
grep won't return a number at end of line


I'm sure this is probably trivial, but it's driving me crazy. It is an exercise for a Linux installation/administration class using Fedora 14. The object is to find lines of text ending in 700 from a text file named datebook. All the lines contain text and numbers ending in a 5 digit number. For this exercise the entry "grep '700$' datebook" returns nothing. I've tried this on a number of text files with lines ending in a character and it works fine. I've also captured 15 lines of lottery numbers and grep will not return any output for an end of line search either. Can anyone give me a clue?
 
Old 10-13-2012, 07:09 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,433

Rep: Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043
Your command works fine for me:
Code:
> cat datebook
dsaf
dsag
fdsagfdsagtret
sdff700
sdflsd4
dsagfdf89efsdf
sfdsf0s0sa
sdfds801
sdfds700
sdfldsihflds811

> grep '700$' datebook
sdff700
sdfds700
Which leads me to believe it might be improper line terminators causing the problem. What is the output of
Code:
file datebook
?

Last edited by suicidaleggroll; 10-13-2012 at 07:12 PM.
 
Old 10-13-2012, 09:42 PM   #3
jgrizich
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the quick response. file datebook returns: datebook: ASCII text, with CRLF line terminators the test file I downloaded that is numbers only returns test1: ASCII English text. It doesn't return the end of line numbers either. I would expect grep to find no problem with the datebook file. I'm still confused. Here are a few lines copied from datebook:

Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
 
Old 10-13-2012, 09:47 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,433

Rep: Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043Reputation: 2043
Quote:
Originally Posted by jgrizich View Post
file datebook returns: datebook: ASCII text, with CRLF line terminators
It has DOS-style line terminators which grep doesn't interpret properly.

Run "dos2unix datebook", and then retry your grep.

Last edited by suicidaleggroll; 10-13-2012 at 09:48 PM.
 
2 members found this post helpful.
Old 10-13-2012, 10:06 PM   #5
jgrizich
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Success!! So the instructor probably built the file in a windows environment and copied it into the filesystem. At my level the distinction flew right over my head. My reaction would be to think that plain text isn't platform dependant. Obviously that's not correct. Thank you for the lesson.
 
Old 10-14-2012, 04:34 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,483

Rep: Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864
Please mark as SOLVED once you have a working solution
 
Old 10-14-2012, 09:34 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Quote:
Originally Posted by jgrizich View Post
My reaction would be to think that plain text isn't platform dependant. Obviously that's not correct. Thank you for the lesson.

A "text" file is really just a series of bytes that the system is told to interpret as text characters according to some character encoding scheme, such as ascii, or utf-8 (The most common unicode encoding, and the Linux standard, ascii is also valid utf-8), or cp-1252 (the standard pre-unicode Windows encoding for English).

If you try to read a file created in one encoding with a program set for a different encoding, you tend to get "mojibake", garbled characters. The "interpretation" ends up wrong.

But the problem here is different. Even when the correct encoding is used, dos/windows and unix have traditionally inserted different characters to indicate the end of a "line" of text, in human terms. Unix uses the ascii LINE FEED character (LF, octal 012, often indicated by the backslash symbol '\n'). Dos uses the combination CARRIAGE RETURN+LINE FEED (CRLF, octal 015+012, '\r\n').

Therefore when a unix program reads a dos file, it ends up with an extra invisible CR at the end of each line, and when a dos program reads a unix file, it sees it as having no line endings at all, but with many non-printing LF characters interspersed in the file.

Incidentally, pre-OSX Apple systems used CR only for their line endings, meaning that both dos and unix would see a file as having a single continuous line, and invisible CR characters sprinkled throughout it. But with OSX they've switched to using the unix-style line format.

So bottom line; first make sure you're using the correct encoding, and if the file could have come from a different system, check that you have the correct line endings.
 
1 members found this post helpful.
Old 10-15-2012, 12:20 PM   #8
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,113
Blog Entries: 11

Rep: Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239
Quote:
Originally Posted by jgrizich View Post
...the instructor probably built the file in a windows environment ....
There may be "extra points" for pointing that out to the instructor. (quietly and non-publicly).
 
  


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
[SOLVED] grep with pattern and line number mohitvad Linux - Newbie 1 09-14-2012 04:00 PM
[SOLVED] Trying to number every other line and append those numbers to end of line kmkocot Programming 7 04-23-2010 11:17 AM
Problem with grep matching to end of line icedown Linux - Newbie 12 09-23-2007 05:43 AM
start grep at line number secretlydead Linux - General 2 09-20-2007 06:03 AM
Multi-line return from grep into an array? rose_bud4201 Programming 2 06-14-2005 04:11 PM


All times are GMT -5. The time now is 05:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration