sed + white space

Hi,

What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters.

My idea (without testing)
sed 's/ //g'

Is there a better way?

$ ruby -pne 'gsub(/\s+,\s+/,",")' file
awk '$1=$1' infile
sed 's/[ \t]//g'

To be pedantic, sed cannot remove all white space from a file. Newline characters qualify as white space and sed output will always include at least one.

Pedantic mode off. :wink:

The following tr command will delete whitespace. Perhaps one will suit your needs.

Spaces only:

tr -d ' '

Spaces and tabs:

tr -d ' \t'

All whitespace (including but not limited to spaces, tabs, newlines, carriage returns, form feeds, vertical tabs):

tr -d '[:space:]'

thank goodness i find this page !

please how to remove the white space and replace with _ example file name backup.avi

and file name [2012] backup.avi

these white spaces and [] are annoying how would i run the command

thank you

tell us your sample file name, and your expect updated file name.

bash to encode,

#!/bin/bash
# MINT 9 - FFMPEG - QT-FASTSTART - X264 - MP4

 
    DIR=/var/www/tmp
 
    for i in `find $DIR -type f`; do
ffmpeg -i $i -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre slow -crf 22 -threads 0 $i.mp4 && qt-faststart $i.mp4 $i.qt.mp4 && chmod 777 $i.qt.mp4 && rm $i.mp4 && rm $i && mv $i.qt.mp4 /var/www/uploads/
    done


input has Dream%20Job.mpg %20 which i want to replace with _ so it looks like this Dream_Job.mpg

then i can feed it to ffmpeg hope you understand

regs
mysoogals

Is this your desired ?

# echo "file name backup.avi" | sed 's/ / [2012] /2'
file name [2012] backup.avi

hmm this is confusing me,

i want to replace white space and with this _

:slight_smile:

Try:

sed 's/[][ \t]/_/g'

thank you, they keep telling but how to use ? lol where do i input file

i try this in terminal Dream%20Job.mpg sed 's/[ \t]/_/g'

this is output :frowning:

linuxmint tmp # ls
Dream%20Job.mpg
linuxmint tmp # Dream%20Job.mpg sed 's/[][ \t]/_/g'
Dream%20Job.mpg: command not found
linuxmint tmp # 

like this?

echo "file name [2012] backup.avi" | sed 's/\[\|\]\| //g'
filename2012backup.avi
$ echo "file name [2012] backup.avi"  | sed 's/[][ \t]/_/g'
file_name__2012__backup.avi

yes but replace white space

i just want to replace this %20 with _ :slight_smile:

$ echo "file name%20bla%20%20backup.avi"  | sed 's/%20/_/g'
file name_bla__backup.avi
# echo "file name [2012] backup.avi" | sed 's/\[\|\]\| /_/g'
file_name__2012__backup.avi

thanks guy for your help, i think i will try to figure this by my self , thanks very much

Or, since you are using bash, you could also use:

var="file name%20bla%20%20backup.avi"
echo "${var//%20/_}"