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 |
Quote:
|
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). |
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. |
Quote:
Quote:
|
Quote:
Quote:
Code:
$ cat infile 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. |
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 wasn't the first thing to pop to mind - sed to just plug in the newline seems simplest; no perlre needed.
|
Quote:
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. |
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.") |
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}' |
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 Both the character and the something must be captured in ( ) and given back as \1 and \2 (because all what matches is substituted). |
As usual, this is not the only (sed) solution that achieves the desired result.
|
Quote:
Example, colorize just the greek: Code:
txt="1|1|1| First Line of text ταύτην και ότι here |
All times are GMT -5. The time now is 04:18 AM. |