Sponsored Content
Top Forums UNIX for Dummies Questions & Answers how to replace spaces with '_' in a file? Post 302448326 by jdhahbi on Wednesday 25th of August 2010 08:22:07 PM
Old 08-25-2010
Hi agama
How do I check which type of spaces I have in my file?
Also, can I send you my infile as an attachment so you can examine it?
I think when I do copy and paste the spaces change.
Thank you for your help

---------- Post updated at 05:22 PM ---------- Previous update was at 04:56 PM ----------

Quote:
Originally Posted by drl
Hi.

Both tr and sed worked for me:
Code:
#!/usr/bin/env bash

# @(#) s1    Demonstrate replacement with tr or sed.

# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p printf specimen tr sed
set -o nounset
pe

FILE=${1-data1}

# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"

pl " Results with tr or sed [both worked]:"
# 'SPACETAB/'
# sed -r 's|[     /]+|_|g' $FILE |
tr -s '     /' '_' < $FILE |
tee t1

# Check results.

pl " Comparison with desired results:"
if [ ! -f expected-output.txt -o ! -s expected-output.txt ]
then
  pe " Comparison file \"expected-output.txt\" zero-length or missing."
  exit
fi
if cmp expected-output.txt t1
then
  pe " Passed -- files have same content."
else
  pe " Failed -- files not identical -- detailed comparison follows."
  if diff -b expected-output.txt t1
  then
    pe " Passed by ignoring whitespace differences."
  fi
fi

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
printf - is a shell builtin [bash]
specimen (local) 1.17
tr (GNU coreutils) 6.10
GNU sed version 4.1.5

 || start [ first:middle:last ]
Whole: 5:0:5 of 8 lines in file "data1"
>AluYa5    SINE1/7SL    Homo sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggcggatcacgaggtc
aggagatcgagaccatcccggctaaaacggtgaaaccccgtctctactaaaaatacaaaaaattagccgg
>AluYb8    SINE1/7SL    Homo sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggtggatcatgaggtc
tccgtctca
>AluYb9    SINE1/7SL    Homo sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggtggatcatgaggtc
 || end

-----
 Results with tr or sed [both worked]:
>AluYa5_SINE1_7SL_Homo_sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggcggatcacgaggtc
aggagatcgagaccatcccggctaaaacggtgaaaccccgtctctactaaaaatacaaaaaattagccgg
>AluYb8_SINE1_7SL_Homo_sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggtggatcatgaggtc
tccgtctca
>AluYb9_SINE1_7SL_Homo_sapiens
ggccgggcgcggtggctcacgcctgtaatcccagcactttgggaggccgaggcgggtggatcatgaggtc

-----
 Comparison with desired results:
 Passed -- files have same content.

Best wishes ... cheers, drl

Hi drl
I am not sure how to use your code; can you give some simple steps for me to follow?
Thanks
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 04:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy