LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2024, 04:53 PM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Rep: Reputation: Disabled
Grep multiple fields large file


Greetings
I have a huge file that I'm trying to extract a few fields from followed by a value. For example the file looks like this but huge, but the pattern is the same

$_h7e 6ijrn3ij exceed: 8686738, string ABC/#123/in4j([99, fieldA

I want to extract all matches and display like:
exceed: 8686738, string ABC/#123/in4j([99,
exceed: 683738, string #Pheu/GP/i972j(3i,
Etc...

I'm trying regex like,
egrep -o "exceed: [0-9]*|string *,"

I'm matching exceed: and the numeric value, but not the field string and up until the comma. Suggestions please
Thanks again
 
Old 01-09-2024, 05:33 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Perhaps, the following?

Code:
grep -o -E 'exceed: [0-9]*|string [^,]*'

# or 

grep -o -E 'exceed: [0-9]*, string [^,]*'
The asterisk does nothing on its own in regular expressions or even extended regular expressions. You're perhaps thinking of globbing, which is different.

Compare "man 7 regex" versus "man 7 glob"
 
Old 01-09-2024, 06:31 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
While I'm the self confessed #1 fan of regex solutions, maybe a simple cut ?.
Code:
cut -d ' ' -f 3-6
 
Old 01-09-2024, 09:49 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
My first-blush guess here is: "greedy" vs. "non-greedy."

If you use a character like "*" in a regular expression, the default behavior is greedy. In other words, it will not stop at the first match, but will instead "greedily" stop at the last one. Which is probably not what you want in this case.

For instance, the pattern (punctuation added ...) {*,} will try to find the last comma.

Last edited by sundialsvcs; 01-09-2024 at 09:52 PM.
 
Old 01-10-2024, 12:40 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Assuming the data is having set fields, you could also use awk:
Code:
awk -F'[ ,]' '/exceed/{print $3,$4","$6,$7","}' file
 
Old 01-10-2024, 11:28 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,816

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Code:
grep -Eo "(exceed:|string) [^,]*"
If string always follows exceed: then you can print the one string:
Code:
grep -Eo "exceed: [0-9]+, string [^,]*"
Code:
grep -Eo "exceed: [0-9]+, string [^,]*,?"
 
  


Reply



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
awk: compare fields of file 1 to fields of file 2 beca123456 Linux - Newbie 2 05-01-2016 03:28 AM
How to compare two files using four fields and print out the mismatched fields ? avatar007 Programming 3 05-04-2014 11:35 AM
[SOLVED] deleting fields in lines that have more fields than the average patolfo Linux - Software 4 09-14-2011 11:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 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
Open Source Consulting | Domain Registration