Replace every other space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace every other space
# 1  
Old 03-11-2010
Network Replace every other space

I'd like a sed command to replace every other space in my file.

File:

0 1 0 3 0 2 0 5

Want:

01 03 02 05

Does anyone have any ideas?
# 2  
Old 03-11-2010
awk '{ print $1 $2, $3 $4, $5 $6} ' olfdile > newfile
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 03-11-2010
or along the similar lines:
Code:
echo '0 1 0 3 0 2 0 5' | nawk '{for(;++i<=NF;) printf("%s%c", $i, (i%2)?"":(i==NF)?ORS:OFS)}'

# 4  
Old 03-11-2010
Code:
$ echo '0 1 0 3 0 2 0 5' | sed "s/[^ ]$/& /;s/ \([^ ]*\) /\1 /g"
01 03 02 05

# 5  
Old 03-11-2010
Code:
echo '0 1 0 3 0 2 0 5' |awk '{for (i=1;i<=NF;i++) {printf ((i%2)?FS:S) $i}}'

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 03-11-2010
And now, time for some Perl... Smilie

Code:
$ 
$ echo "0 1 0 3 0 2 0 5" | perl -ple 's/(\d) (\d)/$1$2/g'
01 03 02 05
$ 
$ echo "0 1 0 3 0 2 0 5" | perl -ple 's/(.) (.)/$1$2/g'
01 03 02 05
$

tyler_durden

Coming to think of it, sed should work with a similar regex as well...

Code:
$ 
$ echo "0 1 0 3 0 2 0 5" | sed 's/\(.\) \(.\)/\1\2/g'
01 03 02 05
$

# 7  
Old 03-11-2010
Quote:
Originally Posted by durden_tyler
And now, time for some Perl... Smilie

Code:
$ 
$ echo "0 1 0 3 0 2 0 5" | perl -ple 's/(\d) (\d)/$1$2/g'
01 03 02 05
$ 
$ echo "0 1 0 3 0 2 0 5" | perl -ple 's/(.) (.)/$1$2/g'
01 03 02 05
$

tyler_durden

Coming to think of it, sed should work with a similar regex as well...

Code:
$ 
$ echo "0 1 0 3 0 2 0 5" | sed 's/\(.\) \(.\)/\1\2/g'
01 03 02 05
$

If the input has more than one digit
Code:
$  echo '0 1 0 3 0 2 0 5' | sed "s/\([^ ]\{1,\}\) \([^ ]\{1,\}\)/\1\2/g"
01 03 02 05

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 to replace text with space in vi?

Hi, In the vi editor, I could do a search and replace: :%s/work/play/g but how do I do this for a string/text with space? like if I want to replace all text of "come here" with text "go there"? I've tried with quotes, double quotes, back slash, none of them worked. thanks!... (1 Reply)
Discussion started by: seafan
1 Replies

2. UNIX for Dummies Questions & Answers

replace %20 with space

Hi, I need torename filenames with %20 to space in a batch wise.Can anyone help me please. Need it badly Eg. English%20Brochure%20002-1 to be replace to English Brochure 002-1 Thanks a lot Please use and tags when posting code, data or logs etc. to preserve formatting... (8 Replies)
Discussion started by: umapearl
8 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. Programming

Replace one space with nothing

hi, d o g e v o l i want a perl command for the above string which should change to the below dog evol replace one space with nothing and two spaces with one space. Thanks, Amey (3 Replies)
Discussion started by: ameyrk
3 Replies

5. Shell Programming and Scripting

replace space for enter

i have to print in a html file directories like this /home/user /home/user/dir but the problem is that when i us this comand listado=`find $direcreal -type f -print` i get this /home/user /home/user/dir1 i try with sed to replace the space with an enter mostrarlistado=`echo "$listado"... (9 Replies)
Discussion started by: pc03
9 Replies

6. UNIX for Dummies Questions & Answers

Replace slash / with space

Hello there everyone. would like to ask for help if i wish to replace a slash / with space using sed. Original: T/T Result: T T hope someone could help me up, thanks Charles (4 Replies)
Discussion started by: seiksoon
4 Replies

7. Shell Programming and Scripting

Replace long space to become one space?

Hi, i have the log attached. Actually i want the long space just become 1 space left like this : Rgds, (12 Replies)
Discussion started by: justbow
12 Replies

8. Shell Programming and Scripting

Replace space

Hai masters, If a file contains content of 2000 lines, from which i need to remove the first n characters or first n spaces from each line of the file. If suppose to remove n characters or first n spaces from a single line means, just use the command nx. But from the above scenario,... (9 Replies)
Discussion started by: ecearund
9 Replies

9. Shell Programming and Scripting

replace space by _

Hi I need to know how I change the spaces by _ in folders and filder founded by find ex. find . -name "* *" -exec echo {} \; ./test space ./test space/new file.txt ./test space/new file ./test space/untitled folder ./test space/untitled folder/new fileruben ./Backup/backup/Image... (6 Replies)
Discussion started by: ruben.rodrigues
6 Replies

10. Shell Programming and Scripting

Replace , (comma) with space

Hi, what is the better way to replace the , (comma) with a space char? Example:STRING=dir1,dir2,dir3 toSTRING=dir1 dir2 dir3 And.. how to find if in the string there is a comma? Thanks :) (6 Replies)
Discussion started by: mbarberis
6 Replies
Login or Register to Ask a Question