0

I am trying to remove the last space of a file.txt which contains many rows. I just need to remove "only the last space" after the third column/each line. My file looks like this:

    3 180 120
    3 123 145
    6 234 0
    4 122 12

I have been trying with the following script but it does not work, so far. Somebody can help me, please?

#!/bin/bash
var="val1 val2 val3 "
var="${var%"${var##*[![:space:]]}"}"
echo "===$var===" <Antart_csv1_copy.txt> trimmed.txt
theduck
  • 2,589
  • 13
  • 17
  • 23
Nella
  • 37
  • 2

2 Answers2

1

You can use sed:

sed -i -e 's/ $//g' filename.txt
  • -i will make the command inplace (change the original file)
  • -e 's/ $//g' will take regular expression <space><endline> and change it to nothing. Modifier g makes it for all lines in the file

You can try it first without -i and redirect output: sed -e 's/ $//g' filename.txt > trimmed.txt

borievka
  • 636
  • 5
  • 13
  • what if there's more than 1 trailing space ? – nullPointer Nov 28 '19 at 09:40
  • @nullPointer Then the additional spaces will be kept - just how OP asked for: `remove "only the last space"`. – Socowi Nov 28 '19 at 09:51
  • 1
    Good answer. Note that the modifier `g` allows multiple substitutions a line. You don't have to specify it in this case. (It is harmless anyway.) – tshiono Nov 28 '19 at 10:14
  • 1
    @Socowi the question title is like "Trim ending white space...". I'd suggest to simply switch this to `sed -i -e 's/ *$//g' filename.txt` - which is otherwise a good answer – nullPointer Nov 28 '19 at 10:34
  • it did not worked for me, but I already found a workaround: cut the columns with awk and print them into another file. the last space dissapeared. Thanks anyway for your answers – Nella Nov 28 '19 at 10:48
  • @Nella check this. https://repl.it/repls/ImpureSmartCore I am using this sed command for a long time, so maybe you have different input? – borievka Nov 28 '19 at 10:59
  • 1
    @nullPointer if we are playing with the words, then "Trim ending white space" means exactly one space, otherwise, it would state that "Trim ending white spaces". And also on top of that there is in the text: I just need to remove "only the last space". I was answering the question. Your suggestion is not answering the OP question – borievka Nov 28 '19 at 11:04
  • @borievka https://www.wordhippo.com/what-is/the-plural-of/space.html Quoting : _The noun space can be countable or uncountable. In more general, commonly used, contexts, the plural form will also be space._ So, no, "Trim ending white space" does not mean exactly one space – nullPointer Nov 28 '19 at 11:09
0

Another solution removing all trailing spaces from each line :

while read line; do echo "${line%%*( )}" >> trimmed.txt; done < Antart_csv1_copy.txt
nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • I'm a bit confused about the `%%*( )` part. Can you explain that? Wouldn't `${line% *}` do the same thing? – Socowi Nov 28 '19 at 09:48
  • @Socowi, why don't you try with `${line% *}` and see what it does actually ? – nullPointer Nov 28 '19 at 10:37
  • I need only the last space! thanks . I already solved the problem :) – Nella Nov 28 '19 at 10:51
  • Of course I did try `${line% *}` – however, only with one trailing space. Now I see the problem: The shortest match (`%`) for `' *'` is always just `' '`. – Socowi Nov 28 '19 at 16:27