Computing.Net > Forums > Unix > Remove last two space from last record

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Remove last two space from last record

Reply to Message Icon

Name: Atul Goel
Date: October 8, 2009 at 18:35:06 Pacific
OS: Unix
Subcategory: General
Comment:

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;N

Please tell me the command in unix to do that.

Thanks in advance

Regards
Atul



Sponsored Link
Ads by Google

Response Number 1
Name: Atul Goel
Date: October 8, 2009 at 22:02:00 Pacific
Reply:

Hi,

I got the solution myself

#get last record

tail -2 $1/Atul_rel.txt > $1/tmp


#remove space from last record

sed 's/[ \t]*$//' $1/tmp > $1/tmp1


#delete last record from main file

sed '$d' $1/Atul_rel.txt > $1/test_rel_1


#append last record to main file

cat $1/test_rel_1 $1/tmp1 > $1/REL_File_test.txt


#delete last record from main file

sed '$d' $1/REL_File_test.txt > $1/REL_File.txt

rm $1/tmp $1/tmp1 $1/test_rel_1 $1/REL_File_test.txt


1

Response Number 2
Name: nails
Date: October 8, 2009 at 22:56:38 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Remove last two space from last record

Remove Last Word in File www.computing.net/answers/unix/remove-last-word-in-file/7535.html

Remove trailing spaces from variabl www.computing.net/answers/unix/remove-trailing-spaces-from-variabl/5179.html

strings www.computing.net/answers/unix/strings/3932.html