sed replace one space and leave other spaces untouched


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace one space and leave other spaces untouched
# 1  
Old 11-06-2013
sed replace one space and leave other spaces untouched

Hi Friends,

I looked up online, but couldn't figure out a proper solution.

I have an input file where the columns are separated by multiple spaces and the column content is separated by single space.

For example,

Code:
Chr1   hello world    unix is fun

In the above example, chr1 is first column, hello world is second column, unix is fun is third column. There are 3 spaces between first and second column, and 4 spaces between second and third columns. My required output is to replace single spaces in the column content only but not the separating space counts.

So, output will be

Code:
Chr1   hello_world    unix_is_fun

Please note that I have multiple columns.
# 2  
Old 11-06-2013
Try:
Code:
sed 's/\([^ ]\) \([^ ]\)/\1_\2/g' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-06-2013
for gnu flavor sed, try also:
Code:
sed 's/\b \b/_/g' file

# 4  
Old 11-06-2013
GNU and some other sed understand the older left \< and right \> word boundaries
Code:
sed 's/\> \</_/g' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 and command as I don't want the tab to be converted . commands not working : sed 's/... (5 Replies)
Discussion started by: kamijia83
5 Replies

2. UNIX for Dummies Questions & Answers

Need help with sed replace script to include spaces

I've got a sed shell script I've been using for quite a while to do batch find/replace operations on .xml files containing various types of text entries. This is the script I use: #!/bin/bash while read text_old text_new; do sed_script+="s/$text_old/$text_new/g;" done < trans_old_to_new... (9 Replies)
Discussion started by: Agreppa
9 Replies

3. 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

4. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

5. 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

6. 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

7. Shell Programming and Scripting

How to Use Sed Command to replace white spaces with comma from between two fields - Mayank

SHELL SCRIPT Hi I have a file in the following format Mayank Sushant Dheeraj Kunal ARUN Samir How can i replace the white space in between and replace them with a comma?? The resultant output should be Mayank,Sushant Dheeraj,Kunal ARUN,Samir i tried using sed -e... (8 Replies)
Discussion started by: mayanksargoch
8 Replies

8. Shell Programming and Scripting

sed replace spaces between quotes with a variable

I have lines with: elseif (req.http.host ~ "^(www.)?edificationtube.com$|www.edificationtube.org www.edificationtube.net edificationtube.org www.edificationtube.com edificationtube.net") { elseif (req.http.host ~ "^(www.)?collegecontender.com$|www.collegecontender.com collegecontenders.com... (3 Replies)
Discussion started by: EXT3FSCK
3 Replies

9. 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

10. Shell Programming and Scripting

replace space or spaces in a line of a file with a single :

I am searching while I await a response to this so if it has been asked already I apologize. I have a file with lines in it that look like: bob johnson email@email.org I need it to look like: bob:johnson:email@email.org I am trying to use sed like this: sed -e 's/ /:/g' file >... (5 Replies)
Discussion started by: NewSolarisAdmin
5 Replies
Login or Register to Ask a Question