LinuxQuestions.org
Visit Jeremy's Blog.
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 05-22-2012, 04:24 PM   #1
mitchellfox
LQ Newbie
 
Registered: May 2010
Posts: 5

Rep: Reputation: 0
grepping for an expression, not listing the whole line


How do I grep, or using awk or sed, a file to find and list and expression part of which stays the same and part of which changes. Also, I would like to not have the whole line listed, just the words found?
for example, in the file are many different instances of 'ABC=123456.' ABC stays the same, but 123456 chasnges, both in the number of integers and the integers themselves. And, when I display the results of the grep, I would like it to display each line with ABC=123456 only, not the whole line. If I use grep -o ABC=, I just get ABC=.

Thanks
 
Old 05-22-2012, 04:47 PM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
Code:
grep -o ABC=[0-9]*
Try man grep or info grep.
 
Old 05-22-2012, 05:00 PM   #3
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 184

Rep: Reputation: 25
grep alone probably won't do that.
grep for the lines you want, e.g. "grep ABC= file"
then pipe into a sed command to replace what you don't want with nothing, i.e. "s/ABC=//"
Cheers, Tom.
 
Old 05-22-2012, 07:04 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
If you could show an actual example (input & desired output) it would be better than just trying to describe it.
 
Old 05-23-2012, 08:15 AM   #5
mitchellfox
LQ Newbie
 
Registered: May 2010
Posts: 5

Original Poster
Rep: Reputation: 0
here is an example

Here is an example. There are many lines like the one below in the log file. I have made many substitutions in the example below because it is proprietary information.ABC=XXXXXXXX is all that I want from the lines. ABC is a constant; those aren't the letters but there are three letters that stay the same.What ABC equals in each line is different in each line (it is not XXXXs). And the rest of the information in this line is even more than what is in this example, and it varies from line to line.

Thanks

0150310
ERROR 2012-04-10 15:03:37,556 : , ABC=XXXXXX Title=XXXXXXXXX XXXXXXX: XXXXXXXX XXXXXXX =sysdate Date=20120410150337
 
Old 05-23-2012, 08:17 AM   #6
mitchellfox
LQ Newbie
 
Registered: May 2010
Posts: 5

Original Poster
Rep: Reputation: 0
what I would like it to look like

what I would like it to look like:

ABC=XXXXXXX Title=XXXXXXXXXX Date=XXXXXXXXXXXXX
 
Old 05-23-2012, 08:36 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,953

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by post #1
I would like it to display each line with ABC=123456 only, not the whole line.
Quote:
Originally Posted by post #5
ABC=XXXXXXXX is all that I want from the lines.
Quote:
Originally Posted by post #6
what I would like it to look like:

ABC=XXXXXXX Title=XXXXXXXXXX Date=XXXXXXXXXXXXX
Make up your mind.
The response in #2 should give you all you need to construct a solution.

Last edited by syg00; 05-23-2012 at 08:37 AM.
 
Old 05-23-2012, 09:57 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by cheesus View Post
grep alone probably won't do that.
Yes it will.....
grep works fine for the problem as first stated (try the code given earlier)

Note: the "search term" with grep is a regular expression (Regex) ((or extended Regex, if you use egrep)).
 
Old 05-23-2012, 10:21 AM   #9
Babertje
Member
 
Registered: Jun 2009
Location: Haarlem, The Netherlands
Distribution: Archlinux
Posts: 125

Rep: Reputation: 20
Code:
cat file |grep ABC |awk '{print $6}'
ABC=XXXXXX
or
Code:
$ cat file |awk '/ABC/ {print $6}'
ABC=XXXXXX
 
  


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] sed edit the line above a regex expression corcodelagaze Programming 9 02-25-2012 09:52 AM
Create File Listing in C++ that will generate a line number on every line of code marzrocks Programming 11 04-12-2010 07:10 AM
BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line) elinenbe Programming 2 12-11-2008 02:17 PM
grepping for multi-line output born4linux Programming 1 03-07-2003 03:26 AM
Quotes in command line expression afshin Programming 4 02-24-2003 08:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:12 PM.

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