LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-05-2009, 11:55 AM   #1
sleeper0110
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Rep: Reputation: 0
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.
 
Old 06-05-2009, 12:11 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
just use dos2unix
 
Old 06-05-2009, 12:19 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by sleeper0110 View Post
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.
 
Old 06-05-2009, 12:29 PM   #4
sleeper0110
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
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.
 
Old 06-05-2009, 12:46 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
if you still want to use awk, another way (not tested)
Code:
awk 'BEGIN{RS="\r\n"; ORS="\n"} {print}' file
 
Old 06-05-2009, 03:52 PM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
You could also use 'tr' ... why does it have to be awk ?
 
Old 06-05-2009, 07:50 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by H_TeXMeX_H View Post
why does it have to be awk ?
because one needs to only learn 1 good tool to do the job of many combined?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting Multiple Lines (with newline) using sed or awk hal8000b Programming 1 03-08-2009 06:21 PM
Error in replacing special chars using awk linux_vidhyarthi Linux - General 2 04-09-2008 08:39 PM
Awk newline problem CelticSoul Linux - General 5 05-03-2006 10:00 PM
Bash Script for a newline olefemmy Programming 1 11-12-2004 12:16 AM
problems with newline in awk test Programming 6 05-02-2004 03:33 AM


All times are GMT -5. The time now is 10:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration