sed second occurence error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed second occurence error
# 1  
Old 07-11-2012
sed second occurence error

Hi Guys,

i'm trying to use sed to replace the second occurrence of the 3 digit number in an I.P address and replace it with another value but it errors out. please advise

Code:
$ cat infil
3538,,,,,,ID,ID1,,,,,,,,,,,123.110.24.5

Code:
sed 's/[0-9]\{1,3\}\./x\./\2/' infil
sed: -e expression #1, char 22: unknown option to `s'


same error when i use with -e switch . can some point me where i'm going wrong here
# 2  
Old 07-11-2012
Quote:
Originally Posted by Irishboy24
Hi Guys,

i'm trying to use sed to replace the second occurrence of the 3 digit number in an I.P address and replace it with another value but it errors out. please advise

Code:
$ cat infil
3538,,,,,,ID,ID1,,,,,,,,,,,123.110.24.5

Code:
sed 's/[0-9]\{1,3\}\./x\./\2/' infil
sed: -e expression #1, char 22: unknown option to `s'


same error when i use with -e switch . can some point me where i'm going wrong here
sed's substitute command's format: s/re/repl/flags
Note the absence of a trailing forward-slash delimiter. You want to use a flag value of 2, not \2/.

Although it probably has nothing to do with the error, the dot is not special in the replacement text. You can simply write x. instead of x\.. Pedantically speaking, \. in the replacement text is an undefined sequence. An implementation is allowed to treat it as it pleases; it can treat the backslash specially and quote the dot, or it can treat the backslash literally and replace the matching text with \., or it can abort with a syntax error. Dropping the backslash in the replacement text is the simplest and most portable option.

Regards,
Alister

Last edited by alister; 07-11-2012 at 08:28 PM..
This User Gave Thanks to alister For This Post:
# 3  
Old 07-11-2012
thanks allister. as you pointed out i had 2 issues in there . one was with the extra \ after the replace pattern and the extra \ before the flag of 2nd occurrence. that fixed it .

also, the treatment of \ before the dot was not necessary in this case.

Thanks mate.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - remove begin of line up to the third and including occurence of character

hello. How to remove all characters in a line from first character ( a $ ) until and including the third occurrence of that character ( $ ). Any help is welcome. (10 Replies)
Discussion started by: jcdole
10 Replies

2. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

3. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

4. Shell Programming and Scripting

Delete until Nth occurence (sed, awk)

Hello people, Once more I need your help with SED/AWK I need to delete up to the Nth occurence of a char (from the beggining) and until the Mth occurence of a char (from the end) Example: Input: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z Output: i,j Must delete up to the... (2 Replies)
Discussion started by: drbiloukos
2 Replies

5. Shell Programming and Scripting

sed print between 2 patterns only last occurence

Hi, I have a file, which contains the following log data. I am trying to print fromt he file the following data: I have tried using sed, but I am getting from the first pattern Thanks for your help. (5 Replies)
Discussion started by: sol_nov
5 Replies

6. Shell Programming and Scripting

Number each occurence using sed

hi, I need to number each occurrence of a pattern within a file using sed. Given object 0000 object 111 object 222 I need following 1.object 0000 2.object 111 3.object 222 (5 Replies)
Discussion started by: xerox
5 Replies

7. Shell Programming and Scripting

Sed command to get first occurence of a line in a file

Hi, I need a help in replacing the first occurrence of blank line with data. I used the following command, but its replacing all de occurrences of blank lines in de file. Is there any other method to replace the first occurrence of blank line? Sed /^$/data O Please use code tags! (5 Replies)
Discussion started by: arjun_arippa
5 Replies

8. Shell Programming and Scripting

Change specific occurence with sed

Hello, I have this file. aaa port=1234 time bbb port=2233 name ccc port=4444 name Is there any way with sed to change only the occurence of "port" which comes after section to have as output : (12 Replies)
Discussion started by: rany1
12 Replies

9. Shell Programming and Scripting

sed - remove spaces before 1rst occurence of string

seems easy but havent found in other posts... i want to delete any spaces if found before first occurence of ${AI_RUN} sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
Discussion started by: danmauer
6 Replies

10. Shell Programming and Scripting

Sed and replacing one occurence of pattern

I would like to use sed to replace one occurence of a pattern in a file. When I use the s/// command it replaces all occurences of the pattern in the file. Should I be using something other than sed? Thanks (6 Replies)
Discussion started by: ss9u
6 Replies
Login or Register to Ask a Question