Matching strings can be counted using grep, cat, sed. Matching strings can be sorted and even omitted for matching repetitions using sort and uniq. They can also be highlighted during the string parsing operation via grep. More can be done using awk. But have you ever tried to display the first occurrence of matching line that exactly begins or starts with the given search string?
Our objective is to display only the first line of occurrence that exactly begins with our matching string "blog linux" (shown in bold - 4th line), see below.
# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
blogger and blogger and linux blog linux
blogging linux blog linux redhat blog linux
blogging sysad blogs fedora linux blog linux
blog linux followed by any words
blogs linux sysad blog linux and blog linux
blogs sysad blog fedora blog linux
fedora blog systems blog linux
linux blog linux blog linux
sysad blog linux blog linux
sysad blogs linux blog linux
systems admin blog linux sysad blog linux
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Noticed that the word "blog linux" appeared several times from different lines of that text file. What we only need is the first occurrence of line that started with the word "blog linux" as our matching string.
Here's how to display only those matching line from text file that exactly begins with given search string.
First, we need to sort the input text file like so
# cat testfile.txt | sort | uniq > output.txt
Then, let's look that matching line that begins with our given search string "blog linux". This would be done using linux look binary command.
# look -d "blog linux" output.txt
result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
blog linux followed by any words
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
That is all, enjoy.
Random Articles:
Quick MRTG Install Tips
MRTG with SkyStream DVB Router and Satellite Receiver
Nagios Generic Setup and Install Tips
Disk Space Report using Shell
Monitoring Server Hosts via Munin
Subscription
Categories
- HowTos (612)
- Linux Devices (40)
- Linux Diggs (620)
- Linux News (1541)
- Linux Videos (24)
Recent Posts
Blog Archive
-
▼
2008
(2301)
-
▼
February
(25)
- HowTo: Extract Lines That Started With Matching St...
- HowTo: Delete All Thumbs.db Recursively
- HowTo: Handy Linux Network Diagnostic Tool
- HowTo: Display Lines That Starts With A Given String
- HowTo: Find And Count JPG Files Recursively
- HowTo: Do Awk with Loop
- HowTo: Strip Out First Word of Line from Text Files
- HowTo: Highlight Matching Grep Results
- HowTo: GNOME Commander File Manager
- HowTo: Install Plone CMS on Fedora
- HowTo: Thumbnail A Website From Linux Command Term...
- HowTo: Advanced Two-Pane File Manager
- HowTo: Two-Pane GUI File Manager
- Slash Proc - File System Utilities
- HowTo: Drop Down Linux Terminal Console
- HowTo: Image Magics with ImageMagick
- HowTo: Convert JPG Image Files to MPG Video Files
- HowTo: Alternative Way to Convert GIF to PNG
- HowTo: Lock X While Root With ScreenSaver
- HowTo: Encrypted Bandwidth-Efficient Linux Backup ...
- HowTo: Install Java IcedTea on Fedora 8
- HowTo: Simultaneous Install of Sun Java and Java I...
- HowTo: Install K9Copy on Fedora
- HowTo: More of Fedora Themes Package
- HowTo: European Language Fonts Installation on Fedora
-
▼
February
(25)
Tuesday, February 26, 2008
HowTo: Display Lines That Starts With A Given String
Subscribe to:
Post Comments (Atom)
ILoveTux - howtos and news | About | Contact | TOS | Policy
0 comments:
Post a Comment