Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2012
Registered User
 
Join Date: Oct 2012
Location: United States
Posts: 22
Thanks: 15
Thanked 0 Times in 0 Posts
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
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts
can you post the output of


Code:
head -5 xtr3.rpl

Sponsored Links
    #3  
Old 10-23-2012
Registered User
 
Join Date: Oct 2012
Location: United States
Posts: 22
Thanks: 15
Thanked 0 Times in 0 Posts
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
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 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
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 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
Registered User
 
Join Date: Oct 2012
Location: United States
Posts: 22
Thanks: 15
Thanked 0 Times in 0 Posts
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
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 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

Tags
awk, ofs

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



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