Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code:

TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g')

It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online also does not work..

My file output is:

4531010001            |fname,lastname             |fieldvalue2             |fieldvalue3            

I want remove all the trailing spaces from the file:
4531010001|fname,lastname|fieldvalue2|fieldvalue3

Thanks :slight_smile:

sed -e 's/\s*//g' filename

HTH,
PL

sed "s/\s*\|//g" filename

Sorry, I'm afraid I don't know how to use sed correctly..

I tried your suggestions but the compiler was trying to access the first word (4531010001) inside the file instead of the file itself..

TVXTEMP=$(sed -e 's/\s*//g' ${TVXTEMP})

This is for ksh by the way..

easy way with sed

[root@sistem1lnx tes1]# cat 3
4531010001            |fname,lastname             |fieldvalue2             |fieldvalue3
[root@sistem1lnx tes1]# sed 's/  *//g' 3
4531010001|fname,lastname|fieldvalue2|fieldvalue3

Thank you for your replies! I was able to do workaround on my sql. Instead of fixing the file, I took out the spaces on my sql results and it worked perfectly. :slight_smile:

For future reference, to delete all spaces from a line:

tr -d ' '

Regards,
Alister