Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

OFS in awk

UNIX for Dummies Questions & Answers


Tags
awk, ofs

 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-23-2012
MIA651 MIA651 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 19 April 2016, 1:01 PM EDT
Location: United States
Posts: 33
Thanks: 21
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 Unix and Linux 10-23-2012
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is online now Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 30 September 2016, 2:24 AM EDT
Posts: 3,102
Thanks: 38
Thanked 676 Times in 653 Posts
can you post the output of


Code:
head -5 xtr3.rpl

Sponsored Links
    #3  
Old Unix and Linux 10-23-2012
MIA651 MIA651 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 19 April 2016, 1:01 PM EDT
Location: United States
Posts: 33
Thanks: 21
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 Unix and Linux 10-23-2012
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is online now Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 30 September 2016, 2:24 AM EDT
Posts: 3,102
Thanks: 38
Thanked 676 Times in 653 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 Unix and Linux 10-23-2012
elixir_sinari's Unix or Linux Image
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 16 September 2016, 7:22 AM EDT
Location: India
Posts: 1,413
Thanks: 101
Thanked 497 Times in 474 Posts
If the input records are having fields of fixed length, you are better off using substr in awk .
Sponsored Links
    #6  
Old Unix and Linux 10-23-2012
MIA651 MIA651 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 19 April 2016, 1:01 PM EDT
Location: United States
Posts: 33
Thanks: 21
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 Unix and Linux 10-23-2012
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is online now Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 30 September 2016, 2:24 AM EDT
Posts: 3,102
Thanks: 38
Thanked 676 Times in 653 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
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 02:38 AM.