-2

Apologies if this has been asked before; I could not find any shell-related answers.

I have a text file with white space that has varying amounts of white space in between the strings.

Example:

chafa    libgusb                      libvirt-glib   needle                       simple-scan cryptominisat                libpulsar        lsd                          oxipng                       spice-protocol python                     docker-machine-completion    hss                          nomad                        ruby@2.4 python@2                   doitlive                     hstr                         numpy                        ruby@2.5 readline                   dwarf                        hunspell                     octave                       sdcv sqlite                     eccodes                      imagemagick                  opendbx    sip

Desired output:

chafa libgusb libvirt-glib needle simple-scan cryptominisat libpulsar lsd oxipng spice-protocol python

I've tried a few sed, tr, cut, perl and other attempts but can't seem to cut the variable white space down to a single white space character.

Any help would be greatly appreciated.

serenesat
  • 4,611
  • 10
  • 37
  • 53
  • 6
    Please add your efforts which you have put to solve the problem in your post along with expected output sample too. Let us know then. – RavinderSingh13 Jan 21 '19 at 00:48
  • `sed` should work. Show what you tried. – lurker Jan 21 '19 at 00:52
  • Hi @lurker, I tried this: sed 's/\s\+/ /g' , sed -i 's// /g' , etc – Dirk_Klauser Jan 21 '19 at 01:00
  • @Dirk_Klauser: if you were to edit your question to add some examples of the things you've tried, then we would be able to explain whatever it is that you're misunderstanding. Wouldn't that be more useful than just giving you an answer? – Dave Cross Jan 21 '19 at 10:07

3 Answers3

3

Command-line solution in Perl

$ perl -pe 's/\s+/ /g' original.txt > cleaned.txt
  • The -p option tells Perl to iterate over each line in the input and print the output.
  • The -e option gives Perl the code to run on each line of the input.
  • s/.../.../ is Perl's substitution operator.
  • /g is the global option to the substitution operator.
  • s/\s+/ /g means "find all runs of one of more whitespace characters and replace each one with an empty string".
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
0

Could you please try following(couldn't test it since OP's expected output is not clear).

awk '{gsub(/[[:space:]]+/,OFS)} 1'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Hmm, it outputs the following: annie ? ? ? ? ? ? ? curl-openssl? ? ? ? gearman ? ? ? ? ? ? libgpg-error? ? ? ? mpv ? ? ? ? ? ? ? ? spdlog? ? ? ? ? ? ? vips ansible ? ? ? ? ? ? cython? ? ? ? ? ? ? geoipupdate ? ? ? ? libuv ? ? ? ? ? ? ? node-build? ? ? ? ? sphinx? ? ? ? ? ? ? webp audacious ? ? ? ? ? dartsim ? ? ? ? ? ? git-open? ? ? ? ? ? libxc ? ? ? ? ? ? ? open- – Dirk_Klauser Jan 21 '19 at 00:53
  • @Dirk_Klauser, as already mentioned in comments to your post. EDIT your post with expected output's sample. As it is not clear. – RavinderSingh13 Jan 21 '19 at 00:56
  • @Dirk_Klauser, yes my command it providing correct output(which you have posted in your post) while running. Did you check it on shown sample only? Doesn't look to me both are same your shown Input_file and your above shown output? – RavinderSingh13 Jan 21 '19 at 01:09
0

Try:

list="chafa    libgusb                      libvirt-glib   needle                       simple-scan cryptominisat                libpulsar        lsd                          oxipng                       spice-protocol python                     docker-machine-completion    hss                          nomad                        ruby@2.4 python@2                   doitlive                     hstr                         numpy                        ruby@2.5 readline                   dwarf                        hunspell                     octave                       sdcv sqlite                     eccodes                      imagemagick                  opendbx    sip"

echo "$list" | sed 's/[ ][ ]*/ /g'

chafa libgusb libvirt-glib needle simple-scan cryptominisat libpulsar lsd oxipng spice-protocol python docker-machine-completion hss nomad ruby@2.4 python@2 doitlive hstr numpy ruby@2.5 readline dwarf hunspell octave sdcv sqlite eccodes imagemagick opendbx sip
fmw42
  • 46,825
  • 10
  • 62
  • 80