sed removing everything before the second occurrence of a word
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
sed removing everything before the second occurrence of a word
Hello everyone,
I was playing around with a script and I was attempting to remove all text before the first occurrence of a word, however it looks like all text is being removed before the second occurrence. The carat should tell sed to start at the beginning of the line no?
From what I can see, via eyeball observance, it appears to have done exactly what you intended. Removed text prior to the first occurrence of the word game.
Code:
From the original:
{"stream":{"_id":20261120112,"game":"Super Smash Bros.
Code:
Modification I can detect::
game":"Super Smash Bros.
Perhaps you can highlight one or more other locations where it found and removed text before the word game.
From what I can see, via eyeball observance, it appears to have done exactly what you intended. Removed text prior to the first occurrence of the word game.
Code:
From the original:
{"stream":{"_id":20261120112,"game":"Super Smash Bros.
Code:
Modification I can detect::
game":"Super Smash Bros.
Perhaps you can highlight one or more other locations where it found and removed text before the word game.
Hello rtmistler,
Thanks for the reply, looks like I tried editing my post and accidentally deleted it instead! I have reposed using a different stream that was up and running at this moment, I have also bolded the word game so its easier to see
{"stream":{"_id":20303823856,"game":"Counter-Strike: Global Offensive","viewers":14870,"created_at":"2016-03-19T15:30:39Z","video_height":720,"average_fps":60.7594936709,"delay":0,"is_playlist":false,"_links": {"self":"https://api.twitch.tv/kraken/streams/ibuypower"},"preview":{"small":"http://static-cdn.jtvnw.net/previews-ttv/live_user_ibuypower-80x45.jpg","medium":"http://static-cdn.jtvnw.net/previews-ttv/live_user_ibuypower-320x180.jpg","large":"http://static-cdn.jtvnw.net/previews-ttv/live_user_ibuypower-640x360.jpg","template":"http://static-cdn.jtvnw.net/previews-ttv/live_user_ibuypower-{width}x{height}.jpg"},"channel":{"mature":false,"status":"iBUYPOWER Spring Invite","broadcaster_language":"en","display_name":"iBUYPOWER","game":"Counter-Strike: Global Offensive","language":"en","_id":82430341,"name":"ibuypower","created_at":"2015-02-11T22:30:37Z","updated_at":"2016-03-19T17:03:33Z","delay":null,"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/ibuypower-profile_image-3cc840e53f9e5387-300x300.jpeg","banner":null,"video_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/ibuypower-channel_offline_image-71079497020bbc2b-1920x1080.jpeg","background":null,"profile_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/ibuypower-profile_banner-9b28ea49c03fd92d-480.jpeg","profile_banner_background_color":null,"partner":true,"url":"https://secure.twitch.tv/ibuypower","views":4493701,"followers":70458,"_links":{"self":"http://api.twitch.tv/kraken/channels/ibuypower","follows":"http://api.twitch.tv/kraken/channels/ibuypower/follows","commercial":"http://api.twitch.tv/kraken/channels/ibuypower/commercial","stream_key":"http://api.twitch.tv/kraken/channels/ibuypower/stream_key","chat":"http://api.twitch.tv/kraken/chat/ibuypower","features":"http://api.twitch.tv/kraken/channels/ibuypower/features","subscriptions":"http://api.twitch.tv/kraken/channels/ibuypower/subscriptions","editors":"http://api.twitch.tv/kraken/channels/ibuypower/editors","teams":"http://api.twitch.tv/kraken/channels/ibuypower/teams","videos":"http://api.twitch.tv/kraken/channels/ibuypower/videos"}}},"_links":{"self":"https://api.twitch.tv/kraken/streams/ibuypower","channel":"https://api.twitch.tv/kraken/channels/ibuypower"}}
game":"Counter-Strike: Global Offensive","language":"en","_id":82430341,"name":"ibuypower","created_at":"2015-02-11T22:30:37Z","updated_at":"2016-03-19T17:03:33Z","delay":null,"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/ibuypower-profile_image-3cc840e53f9e5387-300x300.jpeg","banner":null,"video_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/ibuypower-channel_offline_image-71079497020bbc2b-1920x1080.jpeg","background":null,"profile_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/ibuypower-profile_banner-9b28ea49c03fd92d-480.jpeg","profile_banner_background_color":null,"partner":true,"url":"https://secure.twitch.tv/ibuypower","views":4493701,"followers":70458,"_links":{"self":"http://api.twitch.tv/kraken/channels/ibuypower","follows":"http://api.twitch.tv/kraken/channels/ibuypower/follows","commercial":"http://api.twitch.tv/kraken/channels/ibuypower/commercial","stream_key":"http://api.twitch.tv/kraken/channels/ibuypower/stream_key","chat":"http://api.twitch.tv/kraken/chat/ibuypower","features":"http://api.twitch.tv/kraken/channels/ibuypower/features","subscriptions":"http://api.twitch.tv/kraken/channels/ibuypower/subscriptions","editors":"http://api.twitch.tv/kraken/channels/ibuypower/editors","teams":"http://api.twitch.tv/kraken/channels/ibuypower/teams","videos":"http://api.twitch.tv/kraken/channels/ibuypower/videos"}}},"_links":{"self":"https://api.twitch.tv/kraken/streams/ibuypower","channel":"https://api.twitch.tv/kraken/channels/ibuypower"}}
I see your problem now, but I don't know the answer. In fact, sed is supposed to not do that unless you use the g at the end for global. I think the regex you're using is overriding that, but I don't know how to force it to then be only one. I think you need to rewrite the regex somehow, and not just a tweak, but instead a rewrite of the choices it would make in its search pattern.
awk is faster, easier than perl, very portable, used in automake configure, can do about anything if you know a little "regular expressions", pattern matching, substutution. arrays are awkward but also high-speed binary search indexed automatically (used wisely - powerful). perl is more for "unix suffed inside a script" and large programs - it's know to change syntax so that programs become unusable (need to be rewritten or need many perl versions and no way to know which script is for what).
Rubbish. How is that easier that what I posted.
gawk is not awk, and it is certainly not mawk. Features are continually being added to gawk that are release specific - similar "problems" that you accuse perl of.
I use [g]awk almost exclusively these days too, but each tool has its place.
awk script from 1990 (or long before) still works today gawk awk mawk - written by authors who dont use "one off" features, and there are very few gnu added features to gawk or mawk, and they aren't features one would be attracted to use publicly.
sh script (ie bash) that avoids (quoting pitfalls) from 1990 (or long before) still works today.
perl is a crisis of "they change how patterns match", "changed who is project leader", etc. authors cannot even rely on the most basic facilities in perl for compatibility next release; leaving authors up the creek with broken code they have to rewrite frequently. "Makefile" that use perl are often the source of build failures. Continual upgrade is required that creates dependancies and upgrade of operating system required.
I'm not going to enter into a debate any further, awk.info speaks for itself. It's an AT&T commandline util like sed, with "lifetime support" "never dies" "for use in engineering , in core of operating systems" in mind.
Last edited by X-LFS-2010; 04-05-2016 at 07:20 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.