LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 03-17-2016, 07:27 PM   #1
forkbomb
LQ Newbie
 
Registered: Mar 2016
Posts: 3

Rep: Reputation: Disabled
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?

curl -i https://api.twitch.tv/kraken/streams/sxswgamingesports | sed 's/^.*game/game/'

game":"Super Smash Bros. Melee","language":"en","_id":84224581,"name":"sxswgamingesports","created_at":"2015-03-02T21:29:01Z","updated_at":"2016-03-18T00:17:02Z","delay":null,"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/sxswgamingesports-profile_image-5f7a41e880dd90c0-300x300.png","banner":null,"video_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/sxswgamingesports-channel_offline_image-419701dd15f8d31e-1920x1080.png","background":null,"profile_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/sxswgamingesports-profile_banner-5f17c249ddaeed96-480.png","profile_banner_background_color":null,"partner":false,"url":"https://secure.twitch.tv/sxswgamingesports","views":993795,"followers":15594,"_links":{"self":"https://api.twitch.tv/kraken/channels/sxswgamingesports","follows":"https://api.twitch.tv/kraken/channels/sxswgamingesports/follows","commercial":"https://api.twitch.tv/kraken/channels/sxswgamingesports/commercial","stream_key":"https://api.twitch.tv/kraken/channels/sxswgamingesports/stream_key","chat":"https://api.twitch.tv/kraken/chat/sxswgamingesports","features":"https://api.twitch.tv/kraken/channels/sxswgamingesports/features","subscriptions":"https://api.twitch.tv/kraken/channels/sxswgamingesports/subscriptions","editors":"https://api.twitch.tv/kraken/channels/sxswgamingesports/editors","teams":"https://api.twitch.tv/kraken/channels/sxswgamingesports/teams","videos":"https://api.twitch.tv/kraken/channels/sxswgamingesports/videos"}}},"_links":{"self":"https://api.twitch.tv/kraken/streams/sxswgamingesports","channel":"https://api.twitch.tv/kraken/channels/sxswgamingesports"}}

curl -i https://api.twitch.tv/kraken/streams/sxswgamingesports

{"stream":{"_id":20261120112,"game":"Super Smash Bros. Melee","viewers":23585,"created_at":"2016-03-17T16:36:54Z","video_height":720,"average_fps":30.806321993,"delay":0,"is_playlist":false,"_links":{ "self":"https://api.twitch.tv/kraken/streams/sxswgamingesports"},"preview":{"small":"http://static-cdn.jtvnw.net/previews-ttv/live_user_sxswgamingesports-80x45.jpg","medium":"http://static-cdn.jtvnw.net/previews-ttv/live_user_sxswgamingesports-320x180.jpg","large":"http://static-cdn.jtvnw.net/previews-ttv/live_user_sxswgamingesports-640x360.jpg","template":"http://static-cdn.jtvnw.net/previews-ttv/live_user_sxswgamingesports-{width}x{height}.jpg"},"channel":{"mature":false,"status":"SXSW Gaming: March 17 - 19, 2016 in Austin, Texas","broadcaster_language":"en","display_name":"SXSWGamingEsports","game":"Super Smash Bros. Melee","language":"en","_id":84224581,"name":"sxswgamingesports","created_at":"2015-03-02T21:29:01Z","updated_at":"2016-03-18T00:17:02Z","delay":null,"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/sxswgamingesports-profile_image-5f7a41e880dd90c0-300x300.png","banner":null,"video_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/sxswgamingesports-channel_offline_image-419701dd15f8d31e-1920x1080.png","background":null,"profile_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/sxswgamingesports-profile_banner-5f17c249ddaeed96-480.png","profile_banner_background_color":null,"partner":false,"url":"https://secure.twitch.tv/sxswgamingesports","views":993795,"followers":15594,"_links":{"self":"https://api.twitch.tv/kraken/channels/sxswgamingesports","follows":"https://api.twitch.tv/kraken/channels/sxswgamingesports/follows","commercial":"https://api.twitch.tv/kraken/channels/sxswgamingesports/commercial","stream_key":"https://api.twitch.tv/kraken/channels/sxswgamingesports/stream_key","chat":"https://api.twitch.tv/kraken/chat/sxswgamingesports","features":"https://api.twitch.tv/kraken/channels/sxswgamingesports/features","subscriptions":"https://api.twitch.tv/kraken/channels/sxswgamingesports/subscriptions","editors":"https://api.twitch.tv/kraken/channels/sxswgamingesports/editors","teams":"https://api.twitch.tv/kraken/channels/sxswgamingesports/teams","videos":"https://api.twitch.tv/kraken/channels/sxswgamingesports/videos"}}},"_links":{"self":"https://api.twitch.tv/kraken/streams/sxswgamingesports","channel":"https://api.twitch.tv/kraken/channels/sxswgamingesports"}}

Last edited by forkbomb; 03-19-2016 at 12:04 PM.
 
Old 03-18-2016, 01:55 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 03-19-2016, 12:13 PM   #3
forkbomb
LQ Newbie
 
Registered: Mar 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
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

curl -i https://api.twitch.tv/kraken/streams/ibuypower

{"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"}}


curl -i https://api.twitch.tv/kraken/streams/ibuypower | sed 's/.*game/game/'


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"}}
 
Old 03-19-2016, 12:55 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 03-19-2016, 09:07 PM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,374

Rep: Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754
As pattern matching in sed is greedy, then the observed behaviour is exactly as expected.
Code:
sed 's/^.*game/game/'
The '^.*game' matches from the start of the line to the last occurrence of 'game' in the line.

Perhaps this will give what you want
Code:
sed 's/^.*\(game.*game\)/\1/'

Last edited by allend; 03-19-2016 at 09:13 PM.
 
Old 03-19-2016, 09:46 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Use perl - it does non-greedy "?".
Code:
perl -pe 's/.*?(game.*)/\1/'
 
Old 03-23-2016, 07:23 PM   #7
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
Quote:
Originally Posted by allend View Post
As pattern matching in sed is greedy, then the observed behaviour is exactly as expected.
Code:
sed 's/^.*game/game/'
The '^.*game' matches from the start of the line to the last occurrence of 'game' in the line.

Perhaps this will give what you want
Code:
sed 's/^.*\(game.*game\)/\1/'
No if you have 3 'game' sub-strings in the input it will also go wrong.

The perl solution (non-greed patten match) is probably the best.

Or as a different method, split the line using 'game' as the separator, then remove the first part!

If 'game' should appear in a specific part of the string, perhaps splitting up the line into the appropriate sections first may be a better solution.
 
Old 04-03-2016, 08:55 PM   #8
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 510

Rep: Reputation: 58
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).

awk has long always been awk.

$ cat file | awk '/game/{ if( g==0 ) sub(".*game","game"); g=1 ; print ; next } { print }'

$ cat file | awk 'BEGIN{FS="";RS=""} {sub("\([^g][^a][^m][^e]\)*game","")} END{print}'

http://awk.info

Last edited by X-LFS-2010; 04-03-2016 at 09:22 PM.
 
Old 04-04-2016, 12:23 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
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.
 
Old 04-05-2016, 07:17 AM   #10
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 510

Rep: Reputation: 58
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.

http://awk.info speaks for itself.

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.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Bash / sed / tr / Linux word occurrence counter and analyzer (e.g. for song lyrics) LXer Syndicated Linux News 0 05-20-2012 04:40 AM
[SOLVED] Using sed to substitute repetition -> single occurrence rm_-rf_windows Linux - General 9 03-18-2012 10:12 AM
[SOLVED] SED and Replacing Specific occurrence or Range of Lines bridrod Linux - Newbie 7 08-27-2009 09:59 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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