Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-02-2011, 05:03 AM
|
#1
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Rep: 
|
awk: remove the last character in the file
Dear Experts,
I have a file with multiple lines inside which looks like:
Code:
aaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccc,
dddddddddddddddd,
eeeeeeeeeeeeeeee,
I want to remove the last comma in the last line. The output file should look like:
Code:
aaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccc,
dddddddddddddddd,
eeeeeeeeeeeeeeee
I tried
Code:
awk '{gsub(/,$/,"");print}' FILENAME
and
Code:
sed 's#[\]$##' FILENAME
Both of these code remove all the comma in the file, which is not what I pursued. So, How could I just remove the last comma simply by awk?
Thanks a lot!
|
|
|
|
11-02-2011, 05:24 AM
|
#2
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Hi,
if the last line is not a blank line, i.e. the line which you want the comma removed then you could try something like:
Code:
sed '$ s/,$//' file
|
|
|
1 members found this post helpful.
|
11-02-2011, 08:32 AM
|
#3
|
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
This GNU awk snippet keeps newlines intact, and removes the final comma even if there are empty lines following it:
Code:
gawk 'BEGIN { RS=",[\t\n\v\f\r ]*[\n\r]+" } { printf("%s%s", nl, $0) ; nl=RT } END { sub(/^\,/, "", nl); printf("%s", nl) }'
The idea is to use a record (line) separator consisting of a comma, optional whitespace, and one or more newlines. Using the automatic variable RT provided by GNU awk, we retain the record separators; we only output it just before the next record. When all records have been output, the comma (if any) is stripped from the final record separator, and the final separator is output.
The end result is that the file stays exactly the same, except when there is a final comma followed by (optional whitespace) and at least one newline; then the comma is stripped away.
Note that if there is no newline after the final comma, i.e. the comma is the last character in the file (except for optional spaces and tabs), it is not stripped. If you suspect you may have such files, better use a slightly more complicated variant that handles that case too:
Code:
gawk 'BEGIN { RS=",[\t\n\v\f\r ]*[\n\r]+" } { printf("%s%s", ln, nl); ln = $0; nl = RT } END { if (length(nl) > 0) printf("%s%s", ln, gensub(/^,/, "", "g", nl)); else printf("%s", gensub(/,([\t\v\f ]*)$/, "\\1", "g", ln)) }'
|
|
|
1 members found this post helpful.
|
11-03-2011, 08:26 AM
|
#4
|
|
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,314
|
Will you feed this to any other application and need the final LF? Otherwise the head command might work too:
Code:
$ head -c -2 FILENAME
But it will remove the comma plus the final LF.
|
|
|
1 members found this post helpful.
|
11-03-2011, 09:47 AM
|
#5
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Original Poster
Rep: 
|
Quote:
Originally Posted by Reuti
Will you feed this to any other application and need the final LF? Otherwise the head command might work too:
Code:
$ head -c -2 FILENAME
But it will remove the comma plus the final LF.
|
This is really smart!! Thanks!!!
|
|
|
|
11-03-2011, 10:19 AM
|
#6
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Original Poster
Rep: 
|
Quote:
Originally Posted by Nominal Animal
This GNU awk snippet keeps newlines intact, and removes the final comma even if there are empty lines following it:
Code:
gawk 'BEGIN { RS=",[\t\n\v\f\r ]*[\n\r]+" } { printf("%s%s", nl, $0) ; nl=RT } END { sub(/^\,/, "", nl); printf("%s", nl) }'
The idea is to use a record (line) separator consisting of a comma, optional whitespace, and one or more newlines. Using the automatic variable RT provided by GNU awk, we retain the record separators; we only output it just before the next record. When all records have been output, the comma (if any) is stripped from the final record separator, and the final separator is output.
The end result is that the file stays exactly the same, except when there is a final comma followed by (optional whitespace) and at least one newline; then the comma is stripped away.
Note that if there is no newline after the final comma, i.e. the comma is the last character in the file (except for optional spaces and tabs), it is not stripped. If you suspect you may have such files, better use a slightly more complicated variant that handles that case too:
Code:
gawk 'BEGIN { RS=",[\t\n\v\f\r ]*[\n\r]+" } { printf("%s%s", ln, nl); ln = $0; nl = RT } END { if (length(nl) > 0) printf("%s%s", ln, gensub(/^,/, "", "g", nl)); else printf("%s", gensub(/,([\t\v\f ]*)$/, "\\1", "g", ln)) }'
|
I got the point, thanks for the detailed explanation!!! I do not even know the variable RT before. It seems a really powerful application.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:39 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|