Reputation: 47
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
Upvotes: 0
Views: 917
Reputation: 4574
Another solution removing all trailing spaces from each line :
while read line; do echo "${line%%*( )}" >> trimmed.txt; done < Antart_csv1_copy.txt
Upvotes: 0
Reputation: 692
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 fileYou can try it first without -i
and redirect output:
sed -e 's/ $//g' filename.txt > trimmed.txt
Upvotes: 1