2

I have a file that looks like

SPT-CL J0000-5748 J000106.23-574536.8   0.275980 -57.760231 0.7554 0.0003 template 1
SPT-CL J0000-5748 J000049.27-574637.3   0.205320 -57.777050 0.7018 0.0002 template 1
SPT-CL J0000-5748 J000059.24-574759.6   0.246850 -57.799889 0.7059 0.0002 template 1
SPT-CL J0000-5748 J000107.36-574648.7   0.280680 -57.780209 0.6981 0.0002 template 1

for a few thousand lines. I need all of the SPT-CL J0000-5748 turned into SPT-CLJ0000-5748. How can I do this very quickly via bash?

1 Answer 1

5

You can use sed:

sed 's/ //' file > newfile

or, if you want the changes to be made directly to the original file, you can use the -i command line flag - either along with a backup file

sed -i.bak 's/ //' file

or without creating a backup if you prefer

sed -i 's/ //' file
4
  • where in this expression is it specified that I only want to remove the whitespace between SPT-CL and J0000-5748?
    – user540997
    Jul 18, 2016 at 22:57
  • The -i.bak option creates a backup copy of the original file before modifying it. This behaviour might be helpful or not, but you should at least mention this and offer the alternative option without backup (-i) IMHO.
    – Byte Commander
    Jul 18, 2016 at 22:57
  • 1
    @jphollowed sed by default only replaces the first match on every line. You would have to add the "global" flag to make it replace every match in a line.
    – Byte Commander
    Jul 18, 2016 at 22:59
  • 1
    @jphollowed it removes the first whitespace in each line, as per your question's title: if your file contains lines for which the first whitespace is not the one between SPT-CL and J0000-5748 then that's easy enough to handle - but a good general principle IMHO is to use the simplest expression that does what you need Jul 18, 2016 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy