Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I want to remove last two spaces from the last record only in a file in UNIX. Note that i dont want to remove from all records.
I have a file like this:
100;aaa;N$$
101;bbb;N$$
102;ccc;N$$Here $ represent Spaces.
I want the output like this:
100;aaa;N$$
101;bbb;N$$
102;ccc;NPlease tell me the command in unix to do that.
Thanks in advance
Regards
Atul

Hi,
I got the solution myself
#get last record
tail -2 $1/Atul_rel.txt > $1/tmp
#remove space from last recordsed 's/[ \t]*$//' $1/tmp > $1/tmp1
#delete last record from main filesed '$d' $1/Atul_rel.txt > $1/test_rel_1
#append last record to main filecat $1/test_rel_1 $1/tmp1 > $1/REL_File_test.txt
#delete last record from main filesed '$d' $1/REL_File_test.txt > $1/REL_File.txt
rm $1/tmp $1/tmp1 $1/test_rel_1 $1/REL_File_test.txt

Two different solutions using sed's line addressing:
# on last line, delete last 2 spaces
sed '$s/ $//' datafile.txt# on the last line, delete all trailing spaces
sed '$s/[ ]*$//' datafile.txt

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |