From recent post of displaying the first occurrence of line that starts with a specified matching string, now instead of the first occurrence, here's a way of stripping out all matching lines that start with the given matching string.
Reproducable Approach
Say for example, we have a text file with the below contents and we need to extract those lines shown in BOLD.
# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my linux sysad blog
daily linux sysad blog
linux systems admin blog
systems admin blog
systems admin blog matching line 2
systems admin blog matching line 3
fedora systems admin blog matching line 4
linux systems admin blog matching line 5
pinoy systems admin blog matching line 6
blogging fedora sysad blog
fedora syad blog
distrowatch and linux distributions
linux systems admin
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Noticed above that the words "systems admin blog" appeared a lot of times from different lines. Our objective is to extract ALL the occurring lines that exactly started with the given query string, which is
Here's how to extract line occurrences that starts with the word "systems admin blog" words. Simply
# grep "^systems admin blog" testfile.txt
Alternatively, using linux pipe
# cat testfile.txt | grep "^systems admin blog"
output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
systems admin blog
systems admin blog matching line 2
systems admin blog matching line 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The above commands comes in useful when you need to extract lines that starts with the given query string from a thousand lines of text files. Simple but useful when managing large text file contents.
Take note that you'll get a different results if you do
# grep "systems admin blog"
That is all folks, enjoy.
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)
Thursday, February 28, 2008
HowTo: Extract Lines That Started With Matching String Using Grep
Subscribe to:
Post Comments (Atom)
ILoveTux - howtos and news | About | Contact | TOS | Policy
0 comments:
Post a Comment