ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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).
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.