|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
can you post the output of Code:
head -5 xtr3.rpl |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
If the input records are having fields of fixed length, you are better off using
substr in
awk .
|
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|