LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 09-25-2012, 01:21 PM   #1
SigmaInigma
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Rep: Reputation: Disabled
Linux NOOB - Questions about regular expressions (grep shell utility)


I am in a college class that is introducing us to Linux. I have never used Linux in my life and I am struggling a bit. It is an online class so it is not easy to talk with the teacher.

I am currently using the laterst version of Ubuntu. I am doing an assignment that deals with using grep.

I am analysing a text file (Virginia.txt) and have to "Find all (and only) records where "Louisa" is used as a complete feature name"

I know the command for this is grep -E '^[0-9]*\|VA\|Louisa\|' Virginia.txt because I asked a friend and it works. Thr output of the command is:

1498507|VA|Louisa|ppl|Louisa|51|109|380130N|0780016W|38.025|-78.00444|||||468|1401||Louisa

The third field is the feature name that I put in bold. Basically I have to give a command that will show all lines in the file Virgina.txt that have the word Louisa in the third field.

Like I said this command works: grep -E '^[0-9]*\|VA\|Louisa\|' Virginia.txt

Here is my question: why does it work? I know grep searches a file for a string of characters but everything after that confuses me. What does the -E flag do? Why do they type ^[0-9]*? What does that mean. I just need someone to explain to me what the command is doing step by step so that I can do this myself.

Sorry if my post is hard to read.

Last edited by SigmaInigma; 09-25-2012 at 01:22 PM.
 
Old 09-25-2012, 02:28 PM   #2
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
I am not a regex guru, but I'll do my best to explain. From grep manpage
Quote:
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)
So -E means that the provided string is a regular expression and not a literal string. Now on the expression, let's get it part by part.
'^[0-9]*' means any number of characters from 0 to 9
'\|' means the character '|' which, without the '\' escape character has a special meaning, it's the pipe symbol. The rest (eg 'Louisa') are literal strings. So in the entry
Code:
1498507|VA|Louisa|ppl|Louisa|51|109|380130N|0780016W|38.025|-78.00444|||||468|1401||Louisa
your expression matches the part in bold.
Hope I helped a bit.
 
Old 09-25-2012, 03:01 PM   #3
SigmaInigma
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Ok...that helped a bit but I still have some questions.

grep -E '^[0-9]*\|VA\|Louisa\|' Virginia.txt

So when you put a \ before a special character like | does that tell the shell that | is a normal character? Do you not have to quote it like \|\? I tried rewriting the command to a form that I understand and it did not work at all.

This is the command I tried:
grep -E '.*\|.*\|Louisa\|' Virginia.txt


This was my thought process:
I do not care what is in the first two fields I just need the third field to say Louisa. So in the first two fields I will put .* because . means any letter/number and * means 0 or as many times. Right?

Well this was not the result I got. After running the command this is the output:

Quote:
1759815|VA|Harris Post Office (historical)|po|Louisa|51|109|380137N|0774555W|38.02694|-77.76528||||||||Lake Anna West
1494691|VA|Harris Store (historical)|locale|Louisa|51|109|375637N|0774439W|37.94361|-77.74417|||||306|||Beaverdam
1494692|VA|Harts Shop (historical)|locale|Louisa|51|109|375947N|0774950W|37.99639|-77.83056|||||364|||Buckner
1467843|VA|Hawkins Creek|stream|Louisa|51|109|375603N|0774521W|37.93417|-77.75583|375300N|0774940W|37.88333|-77.82778||||Buckner
1490341|VA|Hawkwood Dam|dam|Louisa|51|109|380112N|0781142W|38.02|-78.195||||||||Boswells Tavern
1490342|VA|Hawkwood Lake|reservoir|Louisa|51|109|380112N|0781142W|38.02|-78.195||||||||Boswells Tavern
1467945|VA|Hensons Store|locale|Louisa|51|109|375733N|0780859W|37.95917|-78.14972|||||505|||Zion Crossroads
1467980|VA|Hickory Creek|stream|Louisa|51|109|380717N|0775904W|38.12139|-77.98444|380628N|0780250W|38.10778|-78.04722||||Mineral
1468052|VA|Hillcrest Cemetery|cemetery|Louisa|51|109|380201N|0780218W|38.03361|-78.03833||||||||Louisa
1494693|VA|Holladay Bridge|bridge|Louisa|51|109|380638N|0775309W|38.11056|-77.88583||||||||Mineral
1468156|VA|Holly Grove|ppl|Louisa|51|109|374744N|0774853W|37.79556|-77.81472|||||306|||Dabneys
1494694|VA|Holly Grove Church|church|Louisa|51|109|374743N|0774902W|37.79528|-77.81722||||||||Dabneys
1468183|VA|Holy Church of the Living God|church|Louisa|51|109|380522N|0775301W|38.08944|-77.88361||||||||Mineral
1468282|VA|Horsepen Branch|stream|Louisa|51|109|375721N|0774858W|37.95583|-77.81611|375835N|0774734W|37.97639|-77.79278||||Buckner
1468350|VA|Hudson Creek|stream|Louisa|51|109|380030N|0780835W|38.00833|-78.14306|380339N|0781013W|38.06083|-78.17028||||Boswells Tavern
1468376|VA|Humphrey Creek|stream|Louisa|51|109|380446N|0780118W|38.07944|-78.02167|380238N|0780247W|38.04389|-78.04639||||Louisa
1494695|VA|Huon West Mill (historical)|locale|Louisa|51|109|380330N|0780830W|38.05833|-78.14167|||||360|||Boswells Tavern
1494696|VA|Immaculate Conception Catholic Church|church|Louisa|51|109|375735N|0774624W|37.95972|-77.77333||||||||Buckner
1494697|VA|Immaculate Conception Cemetery|cemetery|Louisa|51|109|375733N|0774624W|37.95917|-77.77333||||||||Buckner
1468462|VA|Indian Creek|stream|Louisa|51|109|375345N|0775505W|37.89583|-77.91806|375630N|0775416W|37.94167|-77.90444||||Pendleton
1494698|VA|Indian Creek|stream|Louisa|51|109|375527N|0775613W|37.92417|-77.93694|375839N|0775407W|37.9775|-77.90194|260|||Pendleton
1494699|VA|Interchange 136|crossing|Louisa|51|109|375837N|0781251W|37.97694|-78.21417||||||||Zion Crossroads
1494700|VA|Interchange 143|crossing|Louisa|51|109|375625N|0780632W|37.94028|-78.10889||||||||Ferncliff
1494701|VA|Interchange 159|crossing|Louisa|51|109|374652N|0775320W|37.78111|-77.88889||||||||South Anna
1490335|VA|Izac Dam|dam|Louisa|51|109|380434N|0780846W|38.07611|-78.14611||||||||Boswells Tavern
1502043|VA|Izac Lake|reservoir|Louisa|51|109|380434N|0780846W|38.07611|-78.14611|||||397|||Boswells Tavern
1493132|VA|Jackson|ppl|Louisa|51|109|375220N|0774919W|37.87222|-77.82194|||||410|||Dabneys
1927351|VA|Jackson Magisterial District|civil|Louisa|51|109|375438N|0774622W|37.91056|-77.77278||||||||Buckner
1759816|VA|Jackson Post Office|po|Louisa|51|109|375220N|0774919W|37.87222|-77.82194||||||||Dabneys
1748954|VA|Jerdone Island|island|Louisa|51|109|380030N|0774458W|38.00833|-77.74944||||||||Lake Anna East
1468712|VA|Jerusalem Church|church|Louisa|51|109|380500N|0775323W|38.08333|-77.88972||||||||Mineral
1494702|VA|Jones Cemetery|cemetery|Louisa|51|109|375831N|0780813W|37.97528|-78.13694||||||||Zion Crossroads
1468839|VA|Jones Creek|stream|Louisa|51|109|374623N|0774741W|37.77306|-77.79472|374452N|0774855W|37.74778|-77.81528||||Dabneys
1494703|VA|Keens Mill (historical)|locale|Louisa|51|109|380714N|0775820W|38.12056|-77.97222|||||245|||Mineral
1494704|VA|Kents Mill (historical)|locale|Louisa|51|109|380443N|0780205W|38.07861|-78.03472|||||360|||Louisa
1501942|VA|Lake Anna|reservoir|Louisa|51|109|380047N|0774248W|38.01306|-77.71333|||||250||BGN 1972|Lake Anna East
1498609|VA|Lake Anna Airport (historical)|airport|Louisa|51|109|375756N|0774446W|37.96556|-77.74611|||||355|||Beaverdam
1467266|VA|Lake Gordonsville|reservoir|Louisa|51|109|380506N|0781158W|38.085|-78.19944|||||411|||Boswells Tavern
1490336|VA|Lake Gordonsville Dam|dam|Louisa|51|109|380512N|0781200W|38.08667|-78.2||||||||Boswells Tavern
1490300|VA|Lake Louisa|reservoir|Louisa|51|109|380700N|0780042W|38.11667|-78.01167||||||||Louisa
1469873|VA|Lake Louisa|reservoir|Louisa|51|109|380651N|0780041W|38.11417|-78.01139|||||292|||Louisa
1490299|VA|Lake Louisa Dam|dam|Louisa|51|109|380651N|0780041W|38.11417|-78.01139||||||||Louisa
1474165|VA|Lake Senaham|reservoir|Louisa|51|109|380216N|0781527W|38.03778|-78.2575||||||||Keswick
1494705|VA|Lasley (historical)|ppl|Louisa|51|109|380148N|0780729W|38.03|-78.12472|||||440|||Louisa
1469147|VA|Lasley Church|church|Louisa|51|109|380214N|0780558W|38.03722|-78.09944||||||||Louisa
1469148|VA|Lasley Creek|stream|Louisa|51|109|380001N|0780732W|38.00028|-78.12556|380244N|0780458W|38.04556|-78.08278||||Boswells Tavern
1748955|VA|Laurel Hill Cemetery|cemetery|Louisa|51|109|380234N|0774838W|38.04278|-77.81056||||||||Lake Anna West
1469189|VA|Laurel Hill Church|church|Louisa|51|109|380237N|0774844W|38.04361|-77.81222||||||||Lake Anna West
1469233|VA|Leatherwood Creek|stream|Louisa|51|109|374625N|0774804W|37.77361|-77.80111|374441N|0775111W|37.74472|-77.85306||||Dabneys
1469417|VA|Lickinghole Creek|stream|Louisa|51|109|380408N|0780857W|38.06889|-78.14917|380623N|0780754W|38.10639|-78.13167||||Boswells Tavern
1469630|VA|Little River Church|church|Louisa|51|109|375640N|0774801W|37.94444|-77.80028||||||||Buckner
1490298|VA|Little River Number Four Dam|dam|Louisa|51|109|375400N|0774718W|37.9|-77.78833||||||||Buckner
1490338|VA|Little River Number One Dam|dam|Louisa|51|109|375718N|0775242W|37.955|-77.87833||||||||Pendleton
1469637|VA|Little Rock Creek|stream|Louisa|51|109|375759N|0780759W|37.96639|-78.13306|375652N|0780648W|37.94778|-78.11333||||Zion Crossroads
1493218|VA|Locust Creek|ppl|Louisa|51|109|375200N|0774707W|37.86667|-77.78528|||||362|||Dabneys
1759817|VA|Locust Creek Post Office|po|Louisa|51|109|375200N|0774707W|37.86667|-77.78528||||||||Dabneys
1469807|VA|Long Creek|stream|Louisa|51|109|375611N|0774706W|37.93639|-77.785|375451N|0775150W|37.91417|-77.86389||||Buckner
1469808|VA|Long Creek|stream|Louisa|51|109|380431N|0781032W|38.07528|-78.17556|380620N|0780900W|38.10556|-78.15||||Boswells Tavern
1759818|VA|Long Creek Post Office (historical)|po|Louisa|51|109|375552N|0774903W|37.93111|-77.8175||||||||Buckner
1494706|VA|Longcreek (historical)|ppl|Louisa|51|109|375552N|0774903W|37.93111|-77.8175|||||331|||Buckner
1498507|VA|Louisa|ppl|Louisa|51|109|380130N|0780016W|38.025|-78.00444|||||468|1401||Louisa
1494707|VA|Louisa County|civil|Louisa|51|109|375900N|0775800W|37.98333|-77.96667||||||20325||Pendleton
1480232|VA|Louisa County Airport-Freeman Field|airport|Louisa|51|109|380035N|0775820W|38.00972|-77.97222|||||493|||Mineral
1494708|VA|Louisa County Courthouse|building|Louisa|51|109|380128N|0780014W|38.02444|-78.00389||||||||Louisa
1494709|VA|Louisa County High School|school|Louisa|51|109|380104N|0775512W|38.01778|-77.92||||||||Mineral
1494710|VA|Louisa Dragway (historical)|locale|Louisa|51|109|380353N|0780944W|38.06472|-78.16222||||||||Boswells Tavern
1927381|VA|Louisa Magisterial District|civil|Louisa|51|109|380428N|0780140W|38.07444|-78.02778||||||||Louisa
1494711|VA|Louisa Middle School|school|Louisa|51|109|380047N|0775426W|38.01306|-77.90722||||||||Mineral
1759819|VA|Louisa Post Office|po|Louisa|51|109|380130N|0780016W|38.025|-78.00444||||||||Louisa
1498687|VA|Lous Airport|airport|Louisa|51|109|380740N|0780950W|38.12778|-78.16389|||||480|||Gordonsville
1490334|VA|Lous Lake|reservoir|Louisa|51|109|380742N|0780942W|38.12833|-78.16167||||||||Gordonsville
1469977|VA|Macedonia Church|church|Louisa|51|109|375748N|0780509W|37.96333|-78.08583||||||||Ferncliff
1494712|VA|Mallory (historical)|ppl|Louisa|51|109|375659N|0780929W|37.94972|-78.15806|||||520|||Zion Crossroads
1759820|VA|Mallory Post Office (historical)|po|Louisa|51|109|375659N|0780929W|37.94972|-78.15806||||||||Zion Crossroads
1494713|VA|Mansfield (historical)|ppl|Louisa|51|109|380524N|0775446W|38.09|-77.91278|||||350|||Mineral
1759821|VA|Mansfield Post Office (historical)|po|Louisa|51|109|380524N|0775446W|38.09|-77.91278||||||||Mineral
1748956|VA|Marshall Creek|stream|Louisa|51|109|380555N|0774953W|38.09861|-77.83139|380501N|0775109W|38.08361|-77.8525||||Lake Anna West
1494714|VA|Martin Cemetery|cemetery|Louisa|51|109|375620N|0780741W|37.93889|-78.12806||||||||Zion Crossroads
1748957|VA|Martins Point|cape|Louisa|51|109|380634N|0775423W|38.10944|-77.90639||||||||Mineral
1494715|VA|Mechanicsville (historical)|ppl|Louisa|51|109|380423N|0781155W|38.07306|-78.19861|||||510|||Boswells Tavern
1759822|VA|Mechanicsville Post Office (historical)|po|Louisa|51|109|380423N|0781155W|38.07306|-78.19861||||||||Boswells Tavern
1494716|VA|Medlock|ppl|Louisa|51|109|380101N|0780922W|38.01694|-78.15611|||||400|||Boswells Tavern
1493277|VA|Meltons|ppl|Louisa|51|109|380639N|0780837W|38.11083|-78.14361|||||535|||Boswells Tavern
1759823|VA|Meltons Post Office|po|Louisa|51|109|380639N|0780837W|38.11083|-78.14361||||||||Boswells Tavern
1490329|VA|Meyerton Dam|dam|Louisa|51|109|380548N|0780118W|38.09667|-78.02167||||||||Louisa
1490330|VA|Meyerton Lake|reservoir|Louisa|51|109|380548N|0780118W|38.09667|-78.02167||||||||Louisa
1495945|VA|Mildred Crossing|ppl|Louisa|51|109|380231N|0780245W|38.04194|-78.04583|||||540|||Louisa
1470512|VA|Mill Creek|stream|Louisa|51|109|380352N|0780827W|38.06444|-78.14083|380559N|0780702W|38.09972|-78.11722||||Boswells Tavern
1470574|VA|Millington Creek|stream|Louisa|51|109|380247N|0780832W|38.04639|-78.14222|380456N|0780640W|38.08222|-78.11111||||Boswells Tavern
1470579|VA|Millpond Creek|stream|Louisa|51|109|380119N|0774557W|38.02194|-77.76583|380009N|0775150W|38.0025|-77.86389||||Lake Anna West
1495954|VA|Mineral|ppl|Louisa|51|109|380038N|0775432W|38.01056|-77.90889|||||464|424||Mineral
1927408|VA|Mineral Magisterial District|civil|Louisa|51|109|380054N|0775523W|38.015|-77.92306||||||||Mineral
1494717|VA|Mineral United Methodist Church|church|Louisa|51|109|380028N|0775442W|38.00778|-77.91167||||||||Mineral
1494718|VA|Miners Chapel (historical)|church|Louisa|51|109|380237N|0775307W|38.04361|-77.88528||||||||Mineral
1748958|VA|Mitchell Creek|stream|Louisa|51|109|380459N|0774850W|38.08306|-77.81389|380457N|0775054W|38.0825|-77.84833||||Lake Anna West
1748959|VA|Mitchells Point|cape|Louisa|51|109|380439N|0774855W|38.0775|-77.81528||||||||Lake Anna West
1490333|VA|Mittleman Dam|dam|Louisa|51|109|380742N|0780942W|38.12833|-78.16167||||||||Gordonsville
1748960|VA|Moses Point|cape|Louisa|51|109|380202N|0774617W|38.03389|-77.77139||||||||Lake Anna West
1498610|VA|Mostly Harmless Heliport|airport|Louisa|51|109|375755N|0774640W|37.96528|-77.77778|||||330|||Buckner
1477558|VA|Mount Garland|ppl|Louisa|51|109|375608N|0774844W|37.93556|-77.81222|||||324|||Buckner
1494719|VA|Mount Garland Church|church|Louisa|51|109|375606N|0774845W|37.935|-77.8125||||||||Buckner
1470876|VA|Mount Gillian Church|church|Louisa|51|109|380440N|0775544W|38.07778|-77.92889||||||||Mineral
1470877|VA|Mount Gillian New Site Church|church|Louisa|51|109|380516N|0775458W|38.08778|-77.91611||||||||Mineral
1470901|VA|Mount Hope Church|church|Louisa|51|109|375219N|0774915W|37.87194|-77.82083||||||||Dabneys
1471005|VA|Mount Olivet Church|church|Louisa|51|109|375124N|0774545W|37.85667|-77.7625||||||||Dabneys
1471043|VA|Mount Pleasant Church|church|Louisa|51|109|380438N|0775540W|38.07722|-77.92778||||||||Mineral
1471141|VA|Mount Zion Church|church|Louisa|51|109|374821N|0774646W|37.80583|-77.77944||||||||Dabneys
1494720|VA|Mount Zion School (historical)|school|Louisa|51|109|374821N|0774640W|37.80583|-77.77778||||||||Dabneys
1927414|VA|Mountain Road Magisterial District|civil|Louisa|51|109|375015N|0775314W|37.8375|-77.88722||||||||South Anna
1471383|VA|New Anna Church|church|Louisa|51|109|375350N|0775352W|37.89722|-77.89778||||||||Pendleton
1471465|VA|New Line Church|church|Louisa|51|109|374936N|0775506W|37.82667|-77.91833||||||||South Anna
1490323|VA|Nininger Dam|dam|Louisa|51|109|380312N|0780854W|38.05333|-78.14833||||||||Boswells Tavern
1490324|VA|Nininger Lake|reservoir|Louisa|51|109|380312N|0780854W|38.05333|-78.14833||||||||Boswells Tavern
1748961|VA|No Trespassing Point|cape|Louisa|51|109|380157N|0774808W|38.0325|-77.80222||||||||Lake Anna West
1490325|VA|Nolting Dam|dam|Louisa|51|109|380217N|0781016W|38.03806|-78.17111||||||||Boswells Tavern
1471557|VA|Nolting Pond|reservoir|Louisa|51|109|380217N|0781016W|38.03806|-78.17111|||||367|||Boswells Tavern
1471608|VA|North Fork Hickory Creek|stream|Louisa|51|109|380629N|0780250W|38.10806|-78.04722|380440N|0780554W|38.07778|-78.09833||||Louisa
1471610|VA|North Fork Little River|stream|Louisa|51|109|375717N|0774859W|37.95472|-77.81639|375908N|0775257W|37.98556|-77.8825||||Buckner
1471635|VA|North Prong Beaverdam Creek|stream|Louisa|51|109|380035N|0781657W|38.00972|-78.2825|380244N|0781442W|38.04556|-78.245||||Keswick
1471655|VA|Northeast Creek|stream|Louisa|51|109|375600N|0775751W|37.93333|-77.96417|380119N|0775716W|38.02194|-77.95444||||Pendleton
1471687|VA|Nunn Creek|stream|Louisa|51|109|380422N|0775901W|38.07278|-77.98361|380246N|0780235W|38.04611|-78.04306||||Mineral
1471730|VA|Oak Grove Church|church|Louisa|51|109|380419N|0780728W|38.07194|-78.12444||||||||Louisa
1496032|VA|Oakland|ppl|Louisa|51|109|380518N|0780245W|38.08833|-78.04583|||||470|||Louisa
1494721|VA|Oakland Cemetery|cemetery|Louisa|51|109|380143N|0780043W|38.02861|-78.01194||||||||Louisa
1471781|VA|Oakland Church|church|Louisa|51|109|374529N|0775201W|37.75806|-77.86694||||||||Dabneys
1759824|VA|Oakland Post Office|po|Louisa|51|109|380518N|0780245W|38.08833|-78.04583||||||||Louisa
1471945|VA|Orchid|ppl|Louisa|51|109|374944N|0775344W|37.82889|-77.89556|||||337|||South Anna
1471946|VA|Orchid Lake|reservoir|Louisa|51|109|375003N|0775438W|37.83417|-77.91056|||||338|||South Anna
1471995|VA|Owens Creek|stream|Louisa|51|109|374649N|0775021W|37.78028|-77.83917|374940N|0775504W|37.82778|-77.91778||||Dabneys
1927438|VA|Patrick Henry Magisterial District|civil|Louisa|51|109|375849N|0780518W|37.98028|-78.08833||||||||Ferncliff
1497082|VA|Pendleton|ppl|Louisa|51|109|375941N|0775354W|37.99472|-77.89833|||||470|||Pendleton
1759825|VA|Pendleton Post Office|po|Louisa|51|109|375941N|0775354W|37.99472|-77.89833||||||||Pendleton
1490319|VA|Pettit Dam|dam|Louisa|51|109|375830N|0774936W|37.975|-77.82667||||||||Buckner
1490320|VA|Pettit Lake|reservoir|Louisa|51|109|375830N|0774936W|37.975|-77.82667||||||||Buckner
1472253|VA|Phillipi Church|church|Louisa|51|109|375646N|0775326W|37.94611|-77.89056||||||||Pendleton
1472348|VA|Pine Grove Church|church|Louisa|51|109|375440N|0775140W|37.91111|-77.86111||||||||Buckner
1472502|VA|Pleasant Plain Church|church|Louisa|51|109|380423N|0781346W|38.07306|-78.22944||||||||Boswells Tavern
1477632|VA|Plum Tree|ppl|Louisa|51|109|380045N|0774620W|38.0125|-77.77222|||||317|||Lake Anna West
1748962|VA|Plumtree Cove|bay|Louisa|51|109|380020N|0774553W|38.00556|-77.76472||||||||Lake Anna West
1477634|VA|Poindexter|ppl|Louisa|51|109|380005N|0780842W|38.00139|-78.145|||||398|||Boswells Tavern
1759826|VA|Poindexter Post Office|po|Louisa|51|109|380005N|0780842W|38.00139|-78.145||||||||Boswells Tavern
1490331|VA|Ponda Roche Dam|dam|Louisa|51|109|380718N|0780648W|38.12167|-78.11333||||||||Louisa
1490332|VA|Ponda Roche Lake|reservoir|Louisa|51|109|380718N|0780648W|38.12167|-78.11333||||||||Louisa
1472612|VA|Poore Creek|stream|Louisa|51|109|380159N|0780818W|38.03306|-78.13833|380359N|0780541W|38.06639|-78.09472||||Boswells Tavern
1472831|VA|Providence Church|church|Louisa|51|109|374708N|0775418W|37.78556|-77.905||||||||South Anna
1494722|VA|Pufners Mill (historical)|locale|Louisa|51|109|375854N|0780307W|37.98167|-78.05194|||||300|||Ferncliff
1490321|VA|Purcell Dam|dam|Louisa|51|109|380100N|0780954W|38.01667|-78.165||||||||Boswells Tavern
1490322|VA|Purcell Lake|reservoir|Louisa|51|109|380100N|0780954W|38.01667|-78.165||||||||Boswells Tavern
1748963|VA|Purcells Point|cape|Louisa|51|109|380701N|0775725W|38.11694|-77.95694||||||||Mineral
1495101|VA|Quail|ppl|Louisa|51|109|375547N|0780201W|37.92972|-78.03361|||||475|||Ferncliff
1748964|VA|Red House Point|cape|Louisa|51|109|380705N|0775635W|38.11806|-77.94306||||||||Mineral
1473114|VA|Reedy Creek|stream|Louisa|51|109|375913N|0780526W|37.98694|-78.09056|375647N|0780638W|37.94639|-78.11056||||Ferncliff
1477679|VA|Ribbon|ppl|Louisa|51|109|380248N|0775723W|38.04667|-77.95639|||||409|||Mineral
1748965|VA|Richards Gut|stream|Louisa|51|109|380037N|0774718W|38.01028|-77.78833|380110N|0774733W|38.01944|-77.7925||||Lake Anna West
1473187|VA|Richardson High School|school|Louisa|51|109|375940N|0775746W|37.99444|-77.96278||||||||Pendleton
1473252|VA|Rising Sun Church|church|Louisa|51|109|375954N|0775058W|37.99833|-77.84944||||||||Buckner
1748966|VA|Robbies Island|island|Louisa|51|109|375943N|0774503W|37.99528|-77.75083||||||||Buckner
1473363|VA|Rock Creek|stream|Louisa|51|109|375910N|0780614W|37.98611|-78.10389|375627N|0780747W|37.94083|-78.12972||||Ferncliff
1473365|VA|Rock Creek|stream|Louisa|51|109|380032N|0774349W|38.00889|-77.73028|375737N|0774553W|37.96028|-77.76472||||Lake Anna East
1748967|VA|Rock Island|island|Louisa|51|109|380148N|0774725W|38.03|-77.79028||||||||Lake Anna West
1748972|VA|Rock, The|cape|Louisa|51|109|380054N|0774904W|38.015|-77.81778||||||||Lake Anna West
1473596|VA|Roundabout Creek|stream|Louisa|51|109|375733N|0780130W|37.95917|-78.025|375412N|0780414W|37.90333|-78.07056||||Ferncliff
1473752|VA|Saint Johns Chapel|church|Louisa|51|109|380102N|0780931W|38.01722|-78.15861||||||||Boswells Tavern
1473782|VA|Saint Johns Church|church|Louisa|51|109|380517N|0775245W|38.08806|-77.87917||||||||Mineral
1473824|VA|Saint Marks Church|church|Louisa|51|109|374507N|0774852W|37.75194|-77.81444||||||||Dabneys
1473926|VA|Saint Thomas Church|church|Louisa|51|109|375754N|0774348W|37.965|-77.73||||||||Beaverdam
1473955|VA|Salem Church|church|Louisa|51|109|375201N|0775048W|37.86694|-77.84667||||||||Dabneys
1748968|VA|Scerks Creek|stream|Louisa|51|109|380622N|0775227W|38.10611|-77.87417|380525N|0775254W|38.09028|-77.88167||||Lake Anna West
1474155|VA|Sedges Creek|stream|Louisa|51|109|380309N|0774615W|38.0525|-77.77083|380251N|0774858W|38.0475|-77.81611||||Lake Anna West
1474202|VA|Shady Grove Church|church|Louisa|51|109|374545N|0775217W|37.7625|-77.87139||||||||Dabneys
1494723|VA|Shannon Hills|ppl|Louisa|51|109|375313N|0780133W|37.88694|-78.02583|||||510|||Ferncliff
1493577|VA|Shelfar|ppl|Louisa|51|109|374838N|0775204W|37.81056|-77.86778|||||339|||Dabneys
1474468|VA|Shelfar School|school|Louisa|51|109|374915N|0775257W|37.82083|-77.8825||||||||South Anna
1490305|VA|Sheltons Dam|dam|Louisa|51|109|374736N|0774954W|37.79333|-77.83167||||||||Dabneys
1494724|VA|Sheltons Mill (historical)|locale|Louisa|51|109|374732N|0774955W|37.79222|-77.83194|||||230|||Dabneys
1490306|VA|Sheltons Millpond|reservoir|Louisa|51|109|374736N|0774954W|37.79333|-77.83167||||||||Dabneys
1474490|VA|Sherman Lake|reservoir|Louisa|51|109|375832N|0774935W|37.97556|-77.82639|||||266|||Buckner
1748969|VA|Showboat Creek|stream|Louisa|51|109|380150N|0774820W|38.03056|-77.80556|380233N|0774949W|38.0425|-77.83028||||Lake Anna West
1748970|VA|Showman Island|island|Louisa|51|109|380206N|0774800W|38.035|-77.8||||||||Lake Anna West
1490340|VA|Small Country Lake|reservoir|Louisa|51|109|380112N|0780512W|38.02|-78.08667||||||||Louisa
1490339|VA|Small Dam|dam|Louisa|51|109|380112N|0780512W|38.02|-78.08667||||||||Louisa
1494725|VA|Smiths Mill (historical)|locale|Louisa|51|109|380044N|0774205W|38.01222|-77.70139|||||180|||Lake Anna East
1494726|VA|Smiths Mill (historical)|locale|Louisa|51|109|375617N|0775859W|37.93806|-77.98306|||||270|||Pendleton
1474808|VA|Snelson Creek|stream|Louisa|51|109|375925N|0774201W|37.99028|-77.70028|375726N|0774259W|37.95722|-77.71639||||Beaverdam
1477772|VA|South Anna|ppl|Louisa|51|109|375132N|0775420W|37.85889|-77.90556|||||315|||South Anna
1474831|VA|South Anna Church|church|Louisa|51|109|375150N|0775352W|37.86389|-77.89778||||||||South Anna
1490301|VA|South Anna Number Five Dam|dam|Louisa|51|109|380100N|0781048W|38.01667|-78.18||||||||Boswells Tavern
1490302|VA|South Anna Number Seven Dam|dam|Louisa|51|109|380000N|0781030W|38|-78.175||||||||Boswells Tavern
1759827|VA|South Anna Post Office|po|Louisa|51|109|375132N|0775420W|37.85889|-77.90556||||||||South Anna
1474280|VA|South Branch Fork Creek|stream|Louisa|51|109|375328N|0775824W|37.89111|-77.97333|375257N|0780203W|37.8825|-78.03417||||Pendleton
1474855|VA|South Fork Hickory Creek|stream|Louisa|51|109|380628N|0780251W|38.10778|-78.0475|380308N|0780337W|38.05222|-78.06028||||Louisa
1474945|VA|Spicers Lake|reservoir|Louisa|51|109|375930N|0774936W|37.99167|-77.82667||||||||Buckner
1474980|VA|Spring Branch|stream|Louisa|51|109|380004N|0781159W|38.00111|-78.19972|375824N|0781318W|37.97333|-78.22167||||Boswells Tavern
1475025|VA|Springfield Church|church|Louisa|51|109|375907N|0781352W|37.98528|-78.23111||||||||Zion Crossroads
1748971|VA|Sunning Island|island|Louisa|51|109|380656N|0775507W|38.11556|-77.91861||||||||Mineral
1475388|VA|Swift Millpond|reservoir|Louisa|51|109|375638N|0774809W|37.94389|-77.8025|||||237|||Buckner
1490313|VA|Swifts Dam|dam|Louisa|51|109|375638N|0774809W|37.94389|-77.8025||||||||Buckner
1494727|VA|Swifts Mill|locale|Louisa|51|109|375637N|0774811W|37.94361|-77.80306|||||240|||Buckner
1490314|VA|Swifts Millpond|reservoir|Louisa|51|109|375636N|0774806W|37.94333|-77.80167||||||||Buckner
1494728|VA|Talleys Store (historical)|locale|Louisa|51|109|380146N|0780516W|38.02944|-78.08778|||||510|||Louisa
1475468|VA|Tanyard Branch|stream|Louisa|51|109|380031N|0775939W|38.00861|-77.99417|380144N|0780142W|38.02889|-78.02833||||Mineral
1494729|VA|Taylor Cemetery|cemetery|Louisa|51|109|380538N|0780032W|38.09389|-78.00889||||||||Louisa
1475621|VA|Thelma|ppl|Louisa|51|109|380244N|0781432W|38.04556|-78.24222|||||513|||Boswells Tavern
1475623|VA|Thessalonian Church|church|Louisa|51|109|380117N|0775522W|38.02139|-77.92278||||||||Mineral
1494730|VA|Thomas Jefferson Elementary School|school|Louisa|51|109|380131N|0775953W|38.02528|-77.99806||||||||Mineral
1475694|VA|Three Square|locale|Louisa|51|109|374937N|0774854W|37.82694|-77.815||||||||Dabneys
1477810|VA|Threemile Corner|ppl|Louisa|51|109|375549N|0775159W|37.93028|-77.86639|||||378|||Buckner
1475765|VA|Tomahawk Creek|stream|Louisa|51|109|380626N|0775428W|38.10722|-77.90778|380502N|0775523W|38.08389|-77.92306||||Mineral
1748973|VA|Toms Island|island|Louisa|51|109|380032N|0774728W|38.00889|-77.79111||||||||Lake Anna West
1496323|VA|Trevilians|ppl|Louisa|51|109|380305N|0780423W|38.05139|-78.07306|||||526|||Louisa
1759828|VA|Trevilians Post Office|po|Louisa|51|109|380305N|0780423W|38.05139|-78.07306||||||||Louisa
1475880|VA|Trinity Church|church|Louisa|51|109|380447N|0775257W|38.07972|-77.8825||||||||Mineral
1475990|VA|Turners Creek|stream|Louisa|51|109|375154N|0775233W|37.865|-77.87583|375437N|0775159W|37.91028|-77.86639||||South Anna
1748974|VA|Tyler Point|cape|Louisa|51|109|380109N|0774424W|38.01917|-77.74||||||||Lake Anna East
1476058|VA|Union Church|church|Louisa|51|109|380713N|0780228W|38.12028|-78.04111||||||||Louisa
1497188|VA|Vawter Corner|ppl|Louisa|51|109|380744N|0780052W|38.12889|-78.01444|||||371|||Orange
1494731|VA|Vigor|ppl|Louisa|51|109|375610N|0775850W|37.93611|-77.98056|||||350|||Pendleton
1759829|VA|Vigor Post Office|po|Louisa|51|109|375610N|0775850W|37.93611|-77.98056||||||||Pendleton
1498996|VA|WLSA-FM (Louisa)|tower|Louisa|51|109|380137N|0780105W|38.02694|-78.01806||||||||Louisa
1476229|VA|Waldrop|ppl|Louisa|51|109|380425N|0781207W|38.07361|-78.20194|||||508|||Boswells Tavern
1476230|VA|Waldrop Church|church|Louisa|51|109|375954N|0780434W|37.99833|-78.07611||||||||Ferncliff
1759830|VA|Waldrop Post Office|po|Louisa|51|109|380425N|0781207W|38.07361|-78.20194||||||||Boswells Tavern
1748975|VA|Walkups Cove|bay|Louisa|51|109|380032N|0774419W|38.00889|-77.73861||||||||Lake Anna East
1494732|VA|Waltons Store (historical)|locale|Louisa|51|109|375215N|0775851W|37.87083|-77.98083|||||429|||South Anna
1759831|VA|Waltons Store Post Office (historical)|po|Louisa|51|109|375215N|0775851W|37.87083|-77.98083||||||||South Anna
1477857|VA|Wares Crossroads|ppl|Louisa|51|109|380446N|0775249W|38.07944|-77.88028|||||428|||Mineral
1759832|VA|Wares Crossroads Post Office|po|Louisa|51|109|380446N|0775249W|38.07944|-77.88028||||||||Mineral
1764984|VA|Wares Crossroads Post Office|po|Louisa|51|109|380446N|0775249W|38.07944|-77.88028||||||||Mineral
1476367|VA|Watchtower Church|church|Louisa|51|109|375753N|0774619W|37.96472|-77.77194||||||||Buckner
1494733|VA|Watson Cemetery|cemetery|Louisa|51|109|380039N|0780847W|38.01083|-78.14639||||||||Boswells Tavern
1748976|VA|Ways Point|cape|Louisa|51|109|380037N|0774423W|38.01028|-77.73972||||||||Lake Anna East
1494734|VA|Wesleyan Church|church|Louisa|51|109|380334N|0775425W|38.05944|-77.90694||||||||Mineral
1476543|VA|Weyland Church|church|Louisa|51|109|380346N|0780328W|38.06278|-78.05778||||||||Louisa
1476554|VA|Wheeler Creek|stream|Louisa|51|109|380030N|0780837W|38.00833|-78.14361|380235N|0781422W|38.04306|-78.23944||||Boswells Tavern
1476581|VA|White Creek|stream|Louisa|51|109|380456N|0775843W|38.08222|-77.97861|380314N|0780326W|38.05389|-78.05722||||Mineral
1476686|VA|Whitlock|ppl|Louisa|51|109|380358N|0781441W|38.06611|-78.24472|||||537|||Boswells Tavern
1476688|VA|Whitlock Millpond|lake|Louisa|51|109|380201N|0775224W|38.03361|-77.87333||||||||Lake Anna West
1494735|VA|Wickham Corner|ppl|Louisa|51|109|375455N|0774737W|37.91528|-77.79361|||||345|||Buckner
1494736|VA|Williams Mill (historical)|locale|Louisa|51|109|375944N|0780116W|37.99556|-78.02111|||||330|||Ferncliff
1493813|VA|Willowbrook|ppl|Louisa|51|109|375006N|0774857W|37.835|-77.81583|||||330|||Dabneys
1476802|VA|Wills Memorial Church|church|Louisa|51|109|380317N|0780749W|38.05472|-78.13028||||||||Boswells Tavern
1490347|VA|Woodfolk Brothers Dam Number One|dam|Louisa|51|109|375900N|0775854W|37.98333|-77.98167||||||||Pendleton
1490348|VA|Woodfolk Brothers Dam Number Two|dam|Louisa|51|109|375924N|0775936W|37.99|-77.99333||||||||Pendleton
1490349|VA|Woodfolk Brothers Pond Number Two|reservoir|Louisa|51|109|375924N|0775936W|37.99|-77.99333||||||||Pendleton
1490303|VA|Woolfolks Dam|dam|Louisa|51|109|375936N|0775942W|37.99333|-77.995||||||||Pendleton
1490304|VA|Woolfolks Pond|reservoir|Louisa|51|109|375936N|0775942W|37.99333|-77.995||||||||Pendleton
1477739|VA|Yanceyville|ppl|Louisa|51|109|375617N|0775857W|37.93806|-77.9825|||||284|||Pendleton
1494737|VA|Yanceyville Church|church|Louisa|51|109|375600N|0775850W|37.93333|-77.98056||||||||Pendleton
1490307|VA|Yanceyville Dam|dam|Louisa|51|109|375648N|0775900W|37.94667|-77.98333||||||||Pendleton
1490308|VA|Yanceyville Millpond|reservoir|Louisa|51|109|375648N|0775900W|37.94667|-77.98333||||||||Pendleton
1500084|VA|Zion|ppl|Louisa|51|109|375953N|0781403W|37.99806|-78.23417|||||520|||Zion Crossroads
1474398|VA|Zion Church|church|Louisa|51|109|375932N|0781419W|37.99222|-78.23861||||||||Zion Crossroads
1474409|VA|Zion Crossroads|ppl|Louisa|51|109|375815N|0781312W|37.97083|-78.22|||||541|||Zion Crossroads
1759833|VA|Zion Post Office|po|Louisa|51|109|375953N|0781403W|37.99806|-78.23417||||||||Zion Crossroads
1474424|VA|Zion Traveler Church|church|Louisa|51|109|375644N|0780132W|37.94556|-78.02556||||||||Ferncliff
As you can clearly see this is not the expected result. There is only one line in the whole text file that I need to find. None of the results it popped up even had the right word in the third line. Why did the command I used not work?
 
Old 09-25-2012, 03:38 PM   #4
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
First of all, you don't need to quote a | like \|\. Just \|. That's how escape character works.
Now, a plain '*' means anything. So your modified expression means anything and then somewhere a '|' then another '|' and then 'Louisa'. On the original expression, I think it's the '^' character that narrows it down, but I'm not sure. I don't know what that character does. If you don't care about the first two columns, try something like
Code:
$ grep -E '[a-z]*\|[a-z]*\|Louisa\|'
Ofcourse this works only for lowercase letters (I think), but you get the point.
 
Old 09-25-2012, 04:14 PM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
^ means start at the beginning of the line. With -o as option by grep you can see what the match is.
Code:
bash-4.2$ grep -Eo '.*\|.*\|Louisa\|' virginia.txt
1759815|VA|Harris Post Office (historical)|po|Louisa|
1494691|VA|Harris Store (historical)|locale|Louisa|
 snip
1759833|VA|Zion Post Office|po|Louisa|
1474424|VA|Zion Traveler Church|church|Louisa|

bash-4.2$ grep -Eo '^.*\|.*\|Louisa\|' virginia.txt
1759815|VA|Harris Post Office (historical)|po|Louisa|
1494691|VA|Harris Store (historical)|locale|Louisa|
 snip
1759833|VA|Zion Post Office|po|Louisa|
1474424|VA|Zion Traveler Church|church|Louisa|
As you see adding ^ does not change anything here because a * means any character including |.
So you need a field separator. You want the third field only containing Louisa
^ on the start means start at the beginning any place else it means all except. for example
Code:
grep -Eo '^([^|]*\|){2}Louisa\|' virginia.txt
1498507|VA|Louisa|
Code:
grep     -use grep
-E       -use regular expression
o        -print only matching part
'        -start string
^        -start regular expression at beginning of string
(        -start of block
[^|]*    -* means repeat while true in this case while not | print everything in this case 1498507
\|       -matches a | ( end of first field )
)        -end of block
{2}      -repeat block twice VA is second field closed with |
Louisa\| -look if next characters match Louisa\ if so print everything else print nothing
'        -end string

Last edited by whizje; 09-25-2012 at 04:19 PM. Reason: clarification
 
1 members found this post helpful.
Old 09-25-2012, 04:28 PM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
A $ at the end means at the end of the string so next example matches all lines which end with Louisa.
Code:
grep -E 'Louisa$' virginia.txt
Remember Linux is case sensitive I named the file virginia.txt while you named it Virginia.txt that are 2 different files in Linux.

Last edited by whizje; 09-25-2012 at 04:30 PM.
 
Old 09-25-2012, 04:34 PM   #7
SigmaInigma
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you very much guys; you cleared things up for me =)
 
  


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 regular expressions rahularjun86 Linux - Newbie 5 12-03-2011 05:41 PM
[SOLVED] Regular Expressions and Grep metallica1973 Linux - General 8 03-08-2011 09:17 AM
[SOLVED] Help using wc and grep with regular expressions citygrid Linux - Newbie 12 04-17-2010 03:29 AM
[SOLVED] Why this grep command with regular expressions not working on my system? Andrew Dufresne Linux - Newbie 12 10-01-2009 03:38 PM
Regular expressions using grep linuxmandrake Programming 3 11-16-2005 05:29 PM


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