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. |
|
 |
06-05-2009, 10:55 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2008
Posts: 15
Rep:
|
Awk script - Converting newline chars
I'm writing a script to convert \r\n to \n in awk.
I have the following:
awk ' { sub(/\r/, "") ; print } '
Which works perfectly.
I started out with:
awk ' { sub(/\r\n/, "\n") ; print } '
Which doesn't work at all. I don't understand why the second version doesn't work. To me it reads "Replace \r\n with \n.
Can someone explaine why the second line I posted doesn't work but the first one does?
Thanks.
|
|
|
|
06-05-2009, 11:11 AM
|
#2
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
just use dos2unix
|
|
|
|
06-05-2009, 11:19 AM
|
#3
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
Quote:
Originally Posted by sleeper0110
Can someone explaine why the second line I posted doesn't work but the first one does?
|
Because \n is the Record Separator in awk and it is not taken in account in $0. Hence the pattern \r\n is not found, whereas \r is the last character in the string. Anyway, I second ghostdog: don't reinvent the wheel and try dos2unix, instead.
|
|
|
|
06-05-2009, 11:29 AM
|
#4
|
|
LQ Newbie
Registered: Oct 2008
Posts: 15
Original Poster
Rep:
|
colucix: Thanks. Good explanation. Your suggestion of dos2unix doesn't serve my purposes in this case.
ghostdog74: That doesn't answer the question that was asked. And yes, I know what dos2unix is.
|
|
|
|
06-05-2009, 11:46 AM
|
#5
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
if you still want to use awk, another way (not tested)
Code:
awk 'BEGIN{RS="\r\n"; ORS="\n"} {print}' file
|
|
|
|
06-05-2009, 02:52 PM
|
#6
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,611
|
You could also use 'tr' ... why does it have to be awk ?
|
|
|
|
06-05-2009, 06:50 PM
|
#7
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by H_TeXMeX_H
why does it have to be awk ?
|
because one needs to only learn 1 good tool to do the job of many combined? 
|
|
|
|
| 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 04:23 AM.
|
|
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
|
|