how to replace spaces with '_' in a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to replace spaces with '_' in a file?
# 8  
Old 08-25-2010
Quote:
Originally Posted by agama
Rather than uploading your file, run this command and cut/paste the output:

Code:
head your-file-name-here | od -x -c

This should give us a better idea.

I'm guessing that using single quotes didn't work.

$ head infile.txt | od -x -c
0000000 413e 756c 6159 0935 4953 454e 2f31 5337
> A l u Y a 5 \t S I N E 1 / 7 S
0000020 094c 6f48 6f6d 7320 7061 6569 736e 670a
L \t H o m o s a p i e n s \n g
0000040 6367 6763 6767 6763 6763 7467 6767 7463
g c c g g g c g c g g t g g c t
0000060 6163 6763 6363 6774 6174 7461 6363 6163
c a c g c c t g t a a t c c c a
0000100 6367 6361 7474 6774 6767 6761 6367 6763
g c a c t t t g g g a g g c c g
0000120 6761 6367 6767 6367 6767 7461 6163 6763
a g g c g g g c g g a t c a c g
0000140 6761 7467 0a63 6761 6167 6167 6374 6167
a g g t c \n a g g a g a t c g a
0000160 6167 6363 7461 6363 6763 6367 6174 6161
g a c c a t c c c g g c t a a a
0000200 6361 6767 6774 6161 6361 6363 6763 6374
a c g g t g a a a c c c c g t c
0000220 6374 6174 7463 6161 6161 7461 6361 6161
t c t a c t a a a a a t a c a a
0000240 6161 6161 7474 6761 6363 6767 3e0a 6c41
a a a a t t a g c c g g \n > A l
0000260 5975 3862 5309 4e49 3145 372f 4c53 4809
u Y b 8 \t S I N E 1 / 7 S L \t H
0000300 6d6f 206f 6173 6970 6e65 0a73 6767 6363
o m o s a p i e n s \n g g c c
0000320 6767 6367 6367 6767 6774 6367 6374 6361
g g g c g c g g t g g c t c a c
0000340 6367 7463 7467 6161 6374 6363 6761 6163
g c c t g t a a t c c c a g c a
0000360 7463 7474 6767 6167 6767 6363 6167 6767
c t t t g g g a g g c c g a g g
0000400 6763 6767 6774 6167 6374 7461 6167 6767
c g g g t g g a t c a t g a g g
0000420 6374 740a 6363 7467 7463 6163 3e0a 6c41
t c \n t c c g t c t c a \n > A l
0000440 5975 3962 5309 4e49 3145 372f 4c53 4809
u Y b 9 \t S I N E 1 / 7 S L \t H
0000460 6d6f 206f 6173 6970 6e65 0a73 6767 6363
o m o s a p i e n s \n g g c c
0000500 6767 6367 6367 6767 6774 6367 6374 6361
g g g c g c g g t g g c t c a c
0000520 6367 7463 7467 6161 6374 6363 6761 6163
g c c t g t a a t c c c a g c a
0000540 7463 7474 6767 6167 6767 6363 6167 6767
c t t t g g g a g g c c g a g g
0000560 6763 6767 6774 6167 6374 7461 6167 6767
c g g g t g g a t c a t g a g g
0000600 6374 000a
t c \n
0000603
This User Gave Thanks to jdhahbi For This Post:
# 9  
Old 08-25-2010
Thanks for posting the bit of data. Base on the original output of what did/didn't get replaced, the sed is not replacing the tabs. The whitespace in the file is just tabs and spaces (nothing that might have been suppressed during printing etc.)

The problem lies with how the command is being interpreted, which seems odd since it works for me and apparently several other people.

Rather than sed, try this command:

Code:
awk  '/^>/ { gsub( "[ \t]", "_", $0 ); } { print; }' your-file-name

Maybe it's the implementation of sed in your environment. If this doesn't work Im out of guesses.
# 10  
Old 08-25-2010
Quote:
Originally Posted by agama
Thanks for posting the bit of data. Base on the original output of what did/didn't get replaced, the sed is not replacing the tabs. The whitespace in the file is just tabs and spaces (nothing that might have been suppressed during printing etc.)

The problem lies with how the command is being interpreted, which seems odd since it works for me and apparently several other people.

Rather than sed, try this command:

Code:
awk  '/^>/ { gsub( "[ \t]", "_", $0 ); } { print; }' your-file-name

Maybe it's the implementation of sed in your environment. If this doesn't work Im out of guesses.
it is much better now; I need to replace '/' by '_' also:

Code:
$awk  '/^>/ { gsub( "[ \t]", "_", $0 ); } { print; }' infile.txt 
>AluYa5_SINE1/7SL_Homo_sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggcggatcacgaggtc
aggagatcgagaccatcccggctaaaacggtgaaaccccgtctctactaaaaatacaaaaaattagccgg
>AluYb8_SINE1/7SL_Homo_sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggtggatcatgaggtc
tccgtctca
>AluYb9_SINE1/7SL_Homo_sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggtggatcatgaggtc

thank you very much
# 11  
Old 08-25-2010
I think you've probably figured it out, but for the sake of completeness, to also replace the / with '_' just add a slant (/) to the class of characters:

Code:
awk '  /^>/ { gsub( "[ /\t]", "_", $0 ); } { print; }

Glad it worked.
# 12  
Old 08-26-2010
Code:
awk '/>/{gsub(/ +/,"_");gsub(/\t+/,"_");gsub(/\//,"_");gsub(/_+/,"_")}1' infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I replace a string in file that is in a certain position with spaces?

I am trying to replace the string in position 26 through 35 of the data file with 10 spaces and I want the remaining file to stay as is, the record length is over 900 characters? I am trying to use the AWK and substr but I am not getting it formatted correctly. Before... (6 Replies)
Discussion started by: fnwine1500
6 Replies

2. UNIX for Dummies Questions & Answers

How to replace two or more spaces with one comma?

I'm using sh on hp-ux. I've got a file that looks like this. -5.65 175 -16.17 160 -13.57 270 -51.72 260 -8.30 360 -42.71 460 -.38 375 -.20 375 -4.15 170 -21.53 560 -18.84 360 I'd like to replace all the whitespace between the columns with one comma. I can't... (4 Replies)
Discussion started by: Scottie1954
4 Replies

3. Shell Programming and Scripting

String replace that has spaces

cat rf|nawk '/Use SSL= 0/{n+=1}{if (n==3){sub("Use SSL= 0","Use SSL= 0x1",$0)};print }' > rf2Fails. sed 's/Use SSL= 0/Use SSL= 0x1/g' rf > rf2Fails. In addition, the goal is to ONLY replace the 2nd occurence of the... (15 Replies)
Discussion started by: rfransix
15 Replies

4. Shell Programming and Scripting

Replace with spaces

Hi Guys file:///C:/DOCUME%7E1/c104058/LOCALS%7E1/Temp/moz-screenshot.pngsed 's///g' /source/filename.txt > /destination/filename.txt The above code deletes the characters which are not A-Z, a-z and 0-9, but I wanted to replace it with space without deleting them. Any help is... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

5. Shell Programming and Scripting

replace 2 spaces by one

Dear Friends, I have a flat file from which I want to remove single "space". And, wherever two spaces are provided it should replace it by only one space. E.g. I have N A T I O N A L E D U C A T I O N F O R O R G AN I S A T I ON S I want NATIONAL EDUCATION FOR ORGANISATIONS Please... (5 Replies)
Discussion started by: anushree.a
5 Replies

6. Shell Programming and Scripting

Replace blank spaces with semicolon - text file

Hi all, Been trying to find a solution to this, I'm sure its a sed 1 liner, but I don't know sed well enough to work it out... I have a text file in the following format: 431 666 1332 2665 0.24395 432 670 ... (3 Replies)
Discussion started by: mpcengineering
3 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

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

10. Shell Programming and Scripting

Replace spaces

Hi guys, so I have another issue. Can I use sed to replace spaces in a string or variable with %20 I am having trouble with using curl on URL's containing spaces Thanks! (12 Replies)
Discussion started by: tret
12 Replies
Login or Register to Ask a Question