Matthew.Paul.P
Matthew.Paul.P

Reputation: 23

Remove only single spaces in text file with sed, perl, awk, tr or anything

I have a rather large text file where there is an extra space between every character;

I t   l o o k s   l i k e   t h i s .  

I'd like to remove those extra characters so

It looks like this. 

via the Linux terminal. I can't seem to find anyway to do this without removing all of the whitespaces. I'm willing to try any solution at this point. I'd appreciate any nudge in the right direction.

Upvotes: 2

Views: 2236

Answers (5)

Jotne
Jotne

Reputation: 41456

An awksolution

echo "I t   l o o k s   l i k e   t h i s ." | awk '{for (i=1;i<=NF;i+=2) printf $i;print ""}' FS=""
It looks like this.

Upvotes: 3

mpapec
mpapec

Reputation: 50647

perl -pe 's|(\s+)| " "x (length($1)>1) |ge' file

Upvotes: 1

TW80000
TW80000

Reputation: 1505

As long as it's every other character you want to get rid of, you can use python.

>>> s = "I t   l o o k s   l i k e   t h i s ."
>>> print s[0::2]
It looks like this.

If you wanted to do this for the text file, do the following:

with open("/path/to/file.txt") as f:
    f = f.readlines()

with open("/path/to/new.txt") as g:
    for i in f:
        g.write(str(i)[0::2]+"\n")

Upvotes: 1

Borodin
Borodin

Reputation: 126722

Are you certain that the intermediate characters are spaces? It is most likely that this is a UTF-16 file.

I suggest you use a capable editor to open it as such and convert it to UTF-8.

Upvotes: 3

John Kugelman
John Kugelman

Reputation: 361615

$ echo 'I t   l o o k s   l i k e   t h i s .  ' | sed 's/\(.\) /\1/g'
It looks like this. 

Upvotes: 5

Related Questions