Need help appending a string to a field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help appending a string to a field
# 1  
Old 07-17-2009
Need help appending a string to a field

Hi,

This is driving me nuts, can't think of any easy way to do it.

I need to append a string ".00" only in the third field of a file, and only if it does NOT have a decimal point already

Here is what the file looks like-

1400030846,2,17,POL GENERAL
1400030900,3,14.95,FIC GENERAL

If you look, the first line has no .00 after the 17 and I need all the price fields to have a decimal in them, so if it has none, I need to append .00 to it.

The second line is fine as it contains a decimal.

If you are curious these are inventory files, the fields are- isbn, quantity, price, store section

Thanks!

-Steve
# 2  
Old 07-17-2009
try : ( not tested)
Code:
awk ' BEGIN { FS = ",", OFS = ","} { if ($3 !~ /\./) { $3 = $3".00"} ; print $0}' filename


Last edited by clx; 07-17-2009 at 04:54 PM.. Reason: typo in BEGIN
# 3  
Old 07-17-2009
It looks like it should work, butthrows an error

Thanks,

It sure looks like it should work, but it throws this error-

awk: line 1: syntax error at or near ,
# 4  
Old 07-17-2009
Try this:

Code:
awk -F, '{$3=sprintf("%.2f",$3)}1' OFS="," file

Regards
# 5  
Old 07-17-2009
Quote:
Originally Posted by sfisk
Thanks,

It sure looks like it should work, but it throws this error-

awk: line 1: syntax error at or near ,
There's a comma in there that's supposed to be a semi-colon. Try this:

Code:
awk ' BEGIN { FS = ","; OFS = ","} { if ($3 !~ /\./) { $3 = $3".00"} ; print $0}' filename



---------- Post updated at 01:22 PM ---------- Previous update was at 01:15 PM ----------

Quote:
Originally Posted by Franklin52
Try this:

Code:
awk -F, '{$3=sprintf("%.2f",$3)}1' OFS="," file

Regards
I always read the "awk" posts carefully because I'm trying to become a superawker. I'm having trouble figuring out what the number 1 does in your example. I fooled around with it and it doesn't seem to matter what number is there. Just that one is:

Code:
$ awk -F, '{$3=sprintf("%.2f",$3)}1' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}' OFS="," testfile
$ awk -F, '{$3=sprintf("%.2f",$3)}1' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}2' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}3' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}4' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}23123412341234' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}23123412341234.2345' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL

If you don't mind, I'd appreciate if you (or anyone who knows) could take a second and explain why that is.

Thanks.
# 6  
Old 07-17-2009
It worked, THANKS !

Thanks so much to both of you for your help, saved me lots of trial and error !
# 7  
Old 07-17-2009
Quote:
Originally Posted by mglenney
There's a comma in there that's supposed to be a semi-colon. Try this:

Code:
awk ' BEGIN { FS = ","; OFS = ","} { if ($3 !~ /\./) { $3 = $3".00"} ; print $0}' filename



---------- Post updated at 01:22 PM ---------- Previous update was at 01:15 PM ----------



I always read the "awk" posts carefully because I'm trying to become a superawker. I'm having trouble figuring out what the number 1 does in your example. I fooled around with it and it doesn't seem to matter what number is there. Just that one is:

Code:
$ awk -F, '{$3=sprintf("%.2f",$3)}1' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}' OFS="," testfile
$ awk -F, '{$3=sprintf("%.2f",$3)}1' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}2' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}3' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}4' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}23123412341234' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL
$ awk -F, '{$3=sprintf("%.2f",$3)}23123412341234.2345' OFS="," testfile
1400030846,2,17.00,POL GENERAL
1400030900,3,14.95,FIC GENERAL

If you don't mind, I'd appreciate if you (or anyone who knows) could take a second and explain why that is.

Thanks.
awk evaluates the 1 or any value other then 0 (even a variable with a value that's != 0 or != "") after the brace as true and the prints the current line by default, equivalent to {print}.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

2. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

3. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

4. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

5. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies

6. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

7. Shell Programming and Scripting

Appending a new field at the end in a file

can anyone tell me please ......how to append a new field at the end of a file with the help of sed or some other command in bourne shell (8 Replies)
Discussion started by: amitpta
8 Replies

8. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

9. Shell Programming and Scripting

CSV formatting with prefixing, appending and padding field

Hi I have a very large csv file with some hundreds of thousands of rows of data. The data is in the following format: Up to four alpha numeric characters for the first word. This is either set as 2 characters followed by 2 spaces, or as a single 4character word. This is then followed by an 8... (7 Replies)
Discussion started by: meself
7 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question