LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-19-2019, 04:37 PM   #1
glestwid
LQ Newbie
 
Registered: Apr 2018
Posts: 16

Rep: Reputation: Disabled
Question Can't use SED for extracting value


Hi,

I am trying to use SED for printing specific groups from my CSV file per this Stackexchange post. Here's the command:
Code:
sed -e 's/^(\d+);infrastructure;[^;]*;.+/\1/g' 13-05-2019.csv
But instead of the group value I get this:

Quote:
sed: -e expression #1, char 37: invalid reference \1 on `s' command's RHS
What am I doing wrong? How to write this expression with SED in a correct way?
 
Old 05-19-2019, 06:23 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Don't believe everything you read on the net - although there are some good points in that thread.
For backreferences I think you'll need extended regex - see the manpage.
That will solve this particular issue.
 
Old 05-19-2019, 06:53 PM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
The “\1” in the replacement part of the sed substitution command requires a “\(somexregexp\)” in the search part.
Code:
sed ‘s/\(somethingorother\)blabla/\1blublu/‘
EDIT: According to section 5.4 in the sed manual, simple parentheses without the backslash are used if you switch on extended regular expression using the -E option. Given all the plus signs in your sed command, I guess that this is your intention anyway.

Last edited by berndbausch; 05-19-2019 at 07:04 PM.
 
Old 05-20-2019, 09:35 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Further to what has already been said, you will need -r (extended regex) for the + to work as expected as well

You may also wish to show some input and expected output as someone may also have a better idea / method
 
  


Reply

Tags
regex, sed



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
Use of uninitialized value...of an initialized value? macaswell Programming 1 01-06-2013 09:35 AM
strange value assignments variable = value, value?? ostrow30 Programming 2 07-24-2011 07:59 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
sed not working if value is passed thru a variable containg value suchi_s Programming 7 10-29-2004 07:41 AM
extracting more than one value from a string ganninu Programming 16 12-10-2003 03:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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