Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Remove carriage returns from awk output

Shell Programming and Scripting


Tags
awk

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-13-2017
prooney prooney is offline
Registered User
 
Join Date: Jul 2017
Last Activity: 13 July 2017, 5:49 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Remove carriage returns from awk output

I'm on Linux version 2.6.32-696.3.1.el6.x86_64, using the Ksh shell.

I'm working with the input file:


Code:
John Daggett, 341 King Road, Plymouth MA
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA

My sed script "nameState" changes the 2-character state abbreviation to the full state name:
Code:
s/ CA/, California/
s/ MA/, Massachusetts/
s/ OK/, Oklahoma/
s/ PA/, Pennsylvania/
s/ VA/, Virginia/

I issued the sed and awk command within the command shell in emacs:
Code:
sed -f nameState list | awk -F, '{ print $4 ", " $0 }' > output

when I edit this file in Emacs I see:
Code:
Massachusetts^M, John Daggett, 341 King Road, Plymouth, Massachusetts
 Virginia^M, Alice Ford, 22 East Broadway, Richmond, Virginia
 Oklahoma^M, Orville Thomas, 11345 Oak Bridge Road, Tulsa, Oklahoma
 Pennsylvania^M, Terry Kalkas, 402 Lans Road, Beaver Falls, Pennsylvania
 Massachusetts^M, Eric Adams, 20 Post Road, Sudbury, Massachusetts
 Virginia^M, Hubert Sims, 328A Brook Road, Roanoke, Virginia
 California^M, Amy Wilde, 334 Bayshore Pkwy, Mountain View, California
 Massachusetts^M, Sal Carpenter, 73 6th Street, Boston, Massachusetts

which seems to be the output I want, except that it includes a ^M (carriage return?)

but when I issue the command cat output, I see:-
Code:
, John Daggett, 341 King Road, Plymouth, Massachusetts
, Alice Ford, 22 East Broadway, Richmond, Virginia
, Orville Thomas, 11345 Oak Bridge Road, Tulsa, Oklahoma
, Terry Kalkas, 402 Lans Road, Beaver Falls, Pennsylvania
, Eric Adams, 20 Post Road, Sudbury, Massachusetts
, Hubert Sims, 328A Brook Road, Roanoke, Virginia
, Amy Wilde, 334 Bayshore Pkwy, Mountain View, California
, Sal Carpenter, 73 6th Street, Boston, Massachusetts

Why am I not seeing the state in the first field of this file? How can I get rid of the ^M carriage return that the awk command seems to insert?

Thanks for your help!


Moderator's Comments:
Remove carriage returns from awk output
Please wrap all code, files, input & output/errors in CODE tags.
It makes it easier to read and preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 07-13-2017 at 12:31 PM.. Reason: Added CODE/ICODE tags & tightened up line spaces to save blank lines.
Sponsored Links
    #2  
Old Unix and Linux 07-13-2017
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 25 September 2017, 4:26 PM EDT
Location: Houston, Texas, USA
Posts: 970
Thanks: 0
Thanked 330 Times in 312 Posts

Code:
dos2ux < list | sed -f nameState | awk -F, '{ print $4 ", " $0 }' > output

Sponsored Links
    #3  
Old Unix and Linux 07-13-2017
prooney prooney is offline
Registered User
 
Join Date: Jul 2017
Last Activity: 13 July 2017, 5:49 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you for your solution and for advice on good practices in formatting posts.

At my installation, the utility is called dos2unix
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
TR not removing carriage returns nextyoyoma Shell Programming and Scripting 1 09-11-2012 10:37 PM
removing thousand of carriage returns using sed ml5003 Shell Programming and Scripting 4 06-09-2006 05:22 PM
Removing carriage returns with sed stevefox Shell Programming and Scripting 12 01-26-2006 12:02 AM
Remove Carriage returns between strings in a field acheepi Shell Programming and Scripting 10 09-24-2005 01:19 PM
spaces and carriage returns in 'here documents' hcclnoodles Shell Programming and Scripting 0 04-11-2005 06:49 AM



All times are GMT -4. The time now is 06:55 AM.