LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-28-2024, 10:56 AM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Rep: Reputation: Disabled
newline after each string match


hi i have a file that looks like:

something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"

each time i encounter the string "something" i want everything until the next "something" to be on a new line like

something,weoefr0o[0980123-0] bbnu1 "219E"
something,weo23ef0o[0980123-0] bbnu2 "21e9E"
something,weoeeef0o[0980123-0] bbnu2 "219E"

suggestions please? Thanks again
 
Old 02-28-2024, 11:01 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by bishop2001 View Post
hi i have a file that looks like:

something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"

each time i encounter the string "something" i want everything until the next "something" to be on a new line like

something,weoefr0o[0980123-0] bbnu1 "219E"
something,weo23ef0o[0980123-0] bbnu2 "21e9E"
something,weoeeef0o[0980123-0] bbnu2 "219E"

suggestions please? Thanks again
The suggestion I'd have would be to look at several of your other similar threads dealing with grep/sed/awk, and think about how they apply here. And I'd also suggest (as has been asked before) that you post what YOU have already done/tried, rather than just saying what you want without showing any effort.
 
Old 02-28-2024, 11:22 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Yes, would be nice to try to solve it by yourself. you will feel the sense of success.
Otherwise you can solve it using bash, sed, awk, perl, python or whatever language you prefer (including grep).
 
Old 02-28-2024, 12:20 PM   #4
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Original Poster
Rep: Reputation: Disabled
this looks like it it works unless someone has a better way ?

grep -o -P '(?<=something).*(?=bbnu2)'

trying to add a newline after each time there is a match as the file is huge, looks like

something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E" etc...

Thanks.
 
Old 02-28-2024, 01:23 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by bishop2001 View Post
this looks like it it works unless someone has a better way ?
Code:
grep -o -P '(?<=something).*(?=bbnu2)'
Does it WORK?? If so, then you have solved your problem.
Quote:
trying to add a newline after each time there is a match as the file is huge, looks like
Code:
something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E" etc...
Yes, you posted this earlier...you don't give us any context for this, such as how often you have to do this task, what else you've scripted around that grep, or where you're getting this data from. Personally, I'd look at where the data source is to see if there's a way to export it in a different fashion.
 
Old 02-28-2024, 01:52 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by bishop2001 View Post
this looks like it it works unless someone has a better way ?

grep -o -P '(?<=something).*(?=bbnu2)'

trying to add a newline after each time there is a match as the file is huge, looks like

something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E" etc...

Thanks.
But this differs significantly from your original description of the problem:

Quote:
Originally Posted by bishop2001 View Post
something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E"

each time i encounter the string "something" i want everything until the next "something" to be on a new line
Given the following input file your original rules and the attempt shown above would produce very different results:

Code:
$ cat infile
something,weoefr0o[0980123-0] bbnu1 "1xxxx"something,weo23ef0o[0980123-0] bbnu2 "2xxxx"something,weoeeef0o[0980123-0] bbnu2 "3xxxx"
There is nothing that matches in this line, should I print it or not?
something borrowedsomething blue something old andsomething new
What would you expect the output of this file to look like?

It is always important to clearly define the problem before you begin looking for solutions, otherwise you can never know when you have found something that (always) works.
 
1 members found this post helpful.
Old 02-28-2024, 04:19 PM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Many ways to do that.
Code:
a="something,weoefr0o[0980123-0] bbnu1 "219E"something,weo23ef0o[0980123-0] bbnu2 "21e9E"something,weoeeef0o[0980123-0] bbnu2 "219E""

grep -oP 'something.+?(?=something|$)' <<< "$a"

something,weoefr0o[0980123-0] bbnu1 219E
something,weo23ef0o[0980123-0] bbnu2 21e9E
something,weoeeef0o[0980123-0] bbnu2 219E
 
Old 02-28-2024, 04:41 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
grep wasn't the first thing to pop to mind - sed to just plug in the newline seems simplest; no perlre needed.
 
Old 02-28-2024, 04:46 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by syg00 View Post
grep wasn't the first thing to pop to mind - sed to just plug in the newline seems simplest; no perlre needed.
Indeed; mentioned sed to the OP initially, and the OP has been directed to it in the past, for quite some time. Like this, from seven years ago:
https://www.linuxquestions.org/quest...224/page2.html

...but the OP doesn't seem to want to apply anything they've been told previously, and seems to want others to do things for them.
 
Old 02-28-2024, 05:47 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Incidentally, when solving problems like this I usually turn to the Perl programming language, which was originally designed by a geek named Larry Wall when he decided that "awk" wasn't good enough. It has excellent support for "regular expressions" and is very well-suited to "text file processing" tasks such as the present example.

Perl is a "very full-featured – if a bit quirky – programming language" with an exceptional "contributed software library" called CPAN. I am now convinced that "this library goes on forever." No matter what it is that you are now doing, you'll probably discover that it has already been done – and, done very well indeed. Perl has been referred to as "the Swiss Army Knife® of pragmatic computer programming," and I think that this assessment is quite fair. "This is a serious power tool."

And – if the first line of your script is something like #!/usr/bin/perl, it can now be executed as "a command-line command." Thanks to the magic of what is called "#! – shebang", the shell will silently invoke the Perl interpreter to carry out your programming, and the user will be none the wiser.

The definitive – and definitely "quirky" – website for the Perl community is undoubtedly perlmonks.org. ("Be prepared to encounter 'personalities' ... but they know their stuff.")

Last edited by sundialsvcs; 02-28-2024 at 06:10 PM.
 
Old 02-29-2024, 01:38 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
grep is not really the ideal tool for this, grep is used to search for something, not for creating formatted output. But anyway, if you wish:
Code:
grep -Po 'something([^"]*"){2}'
 
Old 03-02-2024, 04:44 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The following inserts newlines before every something that is not at the beginnning of the line.
Code:
sed -r 's/(.)(something)/\1\n\2/g' filename
A character (.) must be before the something, otherwise it is at the beginning of the line.
Both the character and the something must be captured in ( ) and given back as \1 and \2 (because all what matches is substituted).

Last edited by MadeInGermany; 03-02-2024 at 04:52 AM.
 
Old 03-02-2024, 05:02 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
As usual, this is not the only (sed) solution that achieves the desired result.
 
Old 03-02-2024, 09:09 AM   #14
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Quote:
Perl is a "very full-featured – if a bit quirky – programming language" with an exceptional "contributed software library" called CPAN.
Perl regex is nifty. And every linux distro has perl. I'm starting to use it more.

Example, colorize just the greek:
Code:
txt="1|1|1| First Line of text ταύτην και ότι here
1|1|2| Second Line of text ταύτην και ότι here
1|1|3| Third Line of text ταύτην και ότι here
1|1|4| Fourth Line of text ταύτην και ότι here"

perl -pe 's/([Α-Ωα-ωάΆϐέΈήΉίϊΐΪΊόΌϋύΰΫΎώΏοη].([^a-zA-Z]+))/<font color='#4DD2FF'>\1<\/font>/g' <<< "$txt"

1|1|1| First Line of text <font color=#4DD2FF>ταύτην και ότι </font>here
1|1|2| Second Line of text <font color=#4DD2FF>ταύτην και ότι </font>here
1|1|3| Third Line of text <font color=#4DD2FF>ταύτην και ότι </font>here
1|1|4| Fourth Line of text <font color=#4DD2FF>ταύτην και ότι </font>here
 
  


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
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
match newline, space, newline vincix Programming 13 03-09-2017 03:14 PM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 06:11 PM
Any tool to insert a newline each X characters in a file? neoAKiRAz Programming 5 05-09-2007 07:15 PM

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

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