OFS in awk | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

OFS in awk

UNIX for Dummies Questions & Answers


Tags
awk, ofs

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2012
MIA651 MIA651 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 10 April 2014, 12:11 PM EDT
Location: United States
Posts: 28
Thanks: 20
Thanked 1 Time in 1 Post
OFS in awk

Hello,

I have an issue with adding commas as delimiters in this scenario:


Code:
cat xtr3.rpl|head -5|awk 'BEGIN {OFS=","} {print $1,$2,$3,$4}'

Produces this output:

00530083,0000000471,000000000000.00,000000000000.00
00530085,0000000471,000000000000.00,000000000000.00
00550002,0000000471,000000000000.00,000000000015.00
00550030,0000000471,000000000000.00-000000000007.99,
00550034,0000000471,000000000000.00,000000000000.00

The problem here is that when there is a negative value in the 4th column it doesn't recognize the 4th column due to the fact there is no space between the 3rd and 4th column (as you can see in the 4th row) when that happens.

Can anyone kindly help me out?

Thanks!
Sponsored Links
    #2  
Old 10-23-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 14 November 2014, 7:56 AM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts
can you post the output of


Code:
head -5 xtr3.rpl

Sponsored Links
    #3  
Old 10-23-2012
MIA651 MIA651 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 10 April 2014, 12:11 PM EDT
Location: United States
Posts: 28
Thanks: 20
Thanked 1 Time in 1 Post
Yes, sorry I should've done that from the beginning but here is the output:


Code:
head -5 xtr3.rpl

Outputs:

00530083 0000000471 000000000000.00 000000000000.00
00530085 0000000471 000000000000.00 000000000000.00
00550002 0000000471 000000000000.00 000000000015.00
00550030 0000000471 000000000000.00-000000000007.99
00550034 0000000471 000000000000.00 000000000000.00
    #4  
Old 10-23-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 14 November 2014, 7:56 AM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts
try this... (with some assumption)


Code:
awk -v OFS=, 'NR>5{exit}{gsub("-"," -");print $1,$2,$3,$4}' xtr3.rpl

The Following User Says Thank You to itkamaraj For This Useful Post:
MIA651 (10-23-2012)
Sponsored Links
    #5  
Old 10-23-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
If the input records are having fields of fixed length, you are better off using substr in awk .
Sponsored Links
    #6  
Old 10-23-2012
MIA651 MIA651 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 10 April 2014, 12:11 PM EDT
Location: United States
Posts: 28
Thanks: 20
Thanked 1 Time in 1 Post
You're a genius, itkamaraj!

---------- Post updated at 01:05 PM ---------- Previous update was at 01:03 PM ----------

Elixir, the length will be ultimately shortened with the leading zeros trimmed in the 3rd and 4th column.
Sponsored Links
    #7  
Old 10-23-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 14 November 2014, 7:56 AM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts
By adding zero, you can shortend the 3rd and 4th column


Code:
$ awk -v OFS=, 'NR>4{exit}{gsub("-"," -");print $1,$2,$3+0,$4+0}' input
00530083,0000000471,0,0
00530085,0000000471,0,0
00550002,0000000471,0,15
00550030,0000000471,0,-7.99

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 11:12 PM.