Replace space with !@ using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace space with !@ using sed
# 1  
Old 10-16-2015
Replace space with !@ using sed

Hello
I have a requirement where I need to replace space :61 with !@ :61
Source
:60F:123 :61:151 :61:151 :61:15101

Target
:60F:123 :61:151!@:61:151!@:61:15101

I cant use [:space:] and [:blank:] command as I don't want the tab to be converted .

commands not working :
Code:
sed 's/ \(:61:\)/!#\2/g' file1 > file2
sed 's/ \(:61:\)/!#\(:61:\)/g' file1 > file2

Thanks
# 2  
Old 10-16-2015
Hello kamijia83,

Could you please try following and let us know if this helps you.
Code:
echo ":60F:123 :61:151 :61:151 :61:15101" | sed 's/ :61/!@:61/2;s/ :61/!@:61/2'

Output will be as follows.
Code:
:60F:123 :61:151!@:61:151!@:61:15101

Also this solution is changing the 2nd and 3rd occurrences of :61 as per your sample input, if you have more rules related to your input please do let us know with complete same input and expected output.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-16-2015
Would that work?
Code:
sed 's/ \(:61\)/!@\1/2g' file1 > file2

This User Gave Thanks to Aia For This Post:
# 4  
Old 10-16-2015
Hi Ravinder

Thanks for the reply !! Actually there can be any number of :61 tags

Between :60F tag and : 61 I have a TAB
Code:
:60F:123 :61:151

and then I can have any number of 61 tags with spaces between them
# 5  
Old 10-16-2015
There are a few problems with your attempts. You say you want to change a <space> to !@, but your sed substitutions contain !# instead of !@. You have only one parenthesized expression but you use \2 instead of \1 to refer to what was matched by that parenthesized expression. And, your 2nd sed command is inserting unwanted parentheses in the output.

Any of the following should work:
Code:
sed 's/ \(:61:\)/!@\1/g' file1 > file2
sed 's/ \(:61:\)/!@:61:/g' file1 > file2
sed 's/ :61:/!@:61:/g' file1 > file2

The last of these is the shortest and simplest, so I would probably use it.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 10-16-2015
Thanks a lot Don for your help and making me understand my mistake !!!Really appreciate your help

Have a nice weekend !!

---------- Post updated at 11:32 AM ---------- Previous update was at 11:30 AM ----------

Thanks a lot Aia and Ravinder for your quick responses !!
Was able to resolve the issueSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sed- Replace space in filename by a \

`echo $file | sed 's/ / /g'` Hey guys I want help in converting the spaces in my file names to '\ ' . Example: UK maps --> UK\ maps Could someone please help me. I have tried the following sequences already (none of them work): 1)s/ /\ /g 2)s/ /\\ /g 3)s/ /\\\ /g Can someone... (7 Replies)
Discussion started by: INNSAV1
7 Replies

2. Shell Programming and Scripting

sed the find the third , and replace with a space

Dear All, I am new here. Could anyone help to find a sed script for replace the third "," to " "? input: abc,def,ghi,jkl,mno,pqr,stu,vxz output: abc,def,ghi jkl,mno,pqr,stu,vxz Many thanks. Please use code tags next time for your code and data. (2 Replies)
Discussion started by: mimilaw
2 Replies

3. Shell Programming and Scripting

replace space with the help of sed

Hi, i have below string - mynameis arpit i want output like below - mynameis\ arpit that i am getting from below - temp='mynameis arpit' echo $temp|sed 's//\\ /g' --> mynameis\ arpit now i am doing - (2 Replies)
Discussion started by: thearpit
2 Replies

4. Shell Programming and Scripting

Replace comma with a blank space using SED

Hello everyone, I want to replace all "," (commas) with a blank space My command thus far is: cat test.text | sed -e s/\`//g | awk '{print$1" "$2" "$3}' I'm sure you guys know this, but the SED command that I am using is to get rid of the "`" (tics). which gives me: name ... (5 Replies)
Discussion started by: jayT
5 Replies

5. Shell Programming and Scripting

How to use sed to replace space partial

source "PUT 810 712 0001 ILC AK4 00 0 00 00" It needs to be changed to "PUT,810,712,0001,ILC,AK4 00 0 00 00" Thanks in advance. (6 Replies)
Discussion started by: janex
6 Replies

6. Shell Programming and Scripting

Using sed I want to replace space by newline

Input: -------------------------- 123asd 456sdasda 789a ------------------------- output wanted: --------------------- 123asd 456sdasda 789a ---------------------- I want this by sed in simple way please help (I know by: tr ' ' '\n' < inputfile )I want it by sed only (5 Replies)
Discussion started by: RahulJoshi
5 Replies

7. Shell Programming and Scripting

sed : replace space and end-of-line

Hi ! I'm rather new with sed ... learned a lot already by googling etc ... The following script should replace all spaces and ends-of-lines with "something (see below). #!/bin/bash i=0 while read line do fam="H`printf "%06d" $i`" echo $line | sed -e 's//\t'$fam'\n/g' i=$(($i+1))... (7 Replies)
Discussion started by: jossojjos
7 Replies

8. Shell Programming and Scripting

Replace 2 Occurances of Space (sed)

I have a large file that looks like the below output: system SUNWxwmod X Window System kernel modules system SUNWxwoft X Window System optional fonts system SUNWxwopt X Window System Optional Clients system ... (1 Reply)
Discussion started by: hxman
1 Replies

9. Shell Programming and Scripting

how to replace new line ( \n ) with space or Tab in Sed

Hi, how to replace new line with tab in sed ... i used sed -e 's/\n/\t/g' <filename > but its not working (1 Reply)
Discussion started by: mail2sant
1 Replies

10. Shell Programming and Scripting

gnu sed replace space with new line

please help in making sed singleline command i need to insert dos new line (CR LF) before " 34 matching device(s) found on \\cpu1." " 6 matching device(s) found on \\cpu7." " 102 matching device(s) found on \\mainserver." the problem is that sometimes there are both CR LF before strings and... (1 Reply)
Discussion started by: xserg
1 Replies
Login or Register to Ask a Question