Remove zeros from first field, but print all fields


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove zeros from first field, but print all fields
# 1  
Old 05-13-2014
Remove zeros from first field, but print all fields

Hello Everyone,

I've got a comma-delimited file that looks like this:
Code:
     0012,123  ,456  ,05/12/2014
     0123,525  ,286  ,05/12/2014
     0456,791  ,300  ,05/12/2014
     1095,759  ,300  ,05/12/2014
     1344,576  ,292  ,05/12/2014
     1558,551  ,283  ,05/12/2014
002183719,     ,     ,05/12/2014
     2552,816  ,300  ,05/12/2014

I want to remove the leading zeros in the first field only, but am not sure how to combine printf and print. Here's my code:
Code:
awk -F, '{printf "%d\n", $1}' file1

This strips the zeros but only reports field 1. I would like to report the other fields, too. Thanks in advance for your help.
# 2  
Old 05-13-2014
Code:
 awk -F, '{$1+=0}1' OFS=, myFile

# 3  
Old 05-13-2014
Thanks for the quick reply. Unfortunately this didn't work for all the records. Some entries in field 1 came out like this:
Code:

8.80674e+06, ,295 ,05/12/2014

I should have mentioned that I'm using sh on hp-ux. Thank you!
# 4  
Old 05-13-2014
So - where's the leading zeroes in field 1? Should you be unhappy with the scientific notation, add e.g. CONVFMT="%.0f" in front of the file name.
# 5  
Old 05-13-2014
OK, fair enough that I am unfamiliar with the suggested output as scientific notation. But that's why I posted in this forum, and it's not helpful for me to have an answer that changes my number to something unusable.

So let's back up here -- what I really want to do is use printf to format field $1 and then also print the other fields as well. But I can't figure out how to change just field $1. Learning how to do this can be applied to other printf statements, so I'd appreciate the help.

Thank you.
# 6  
Old 05-13-2014
could you provide a representative 'enough' sample of data - seems like we're dealing with more than just what was provided initially.
# 7  
Old 05-13-2014
Let's look at field $1 in the sample file I provided. The maximum number of digits in this field is 9, although many entries are less than nine digits, and some begin with one or more leading zeros.

My goal is to format field $1 so I can sort the entire file in ascending numeric order by field $1. Maybe printf isn't the best method. Perhaps padding the field with leading zeroes would be an alternative answer. But then I'm in the same boat -- how do I combine the printf formatting with reporting the rest of the records for each line? Again, this may not be the best method. Thank you for your assistance.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print lines based on text in field and value in two additional fields

In the awk below I am trying to print the entire line, along with the header row, if $2 is SNV or MNV or INDEL. If that condition is met or is true, and $3 is less than or equal to 0.05, then in $7 the sub pattern :GMAF= is found and the value after the = sign is checked. If that value is less than... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to remove lines where field count is greather than 1 in two fields

I am trying to remove all the lines and spaces where the count in $4 or $5 is greater than 1 (more than 1 letter). The file and the output are tab-delimited. Thank you :). file X 5811530 . G C NLGN4X 17 10544696 . GA G MYH3 9 96439004 . C ... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

6. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies

7. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

8. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

9. Shell Programming and Scripting

Trim leading zeros to make field 6 characters long

Hi all- I've got a file that will have multiple columns. In one column there will be a string that is 10 digits in length, but I need to trim the first four zeros to make it 6 characters? example: 0000001234 0000123456 0000234566 0000000321 output: 001234 123456 234566 000321 (5 Replies)
Discussion started by: Cailet
5 Replies

10. Shell Programming and Scripting

remove precursing zeros

Hello all, I have a list of reports for stores that are numbered like: s001845,s000022,s198490,s020048,s002385 however the users are displaying the reports as: 1845,22,198490,20048,2385 It isn't real critical but I would like to associate them so they are the same. And since the users are... (2 Replies)
Discussion started by: gozer13
2 Replies
Login or Register to Ask a Question