modify a few field of the record information


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting modify a few field of the record information
# 1  
Old 03-22-2007
modify a few field of the record information

Hello,
I have the following record in a text file, i would like modify some field:
1 - remove all space between ",", but the company name of word will not delete. Anyway, I can use the following statement to do it.
's/^ *//;s/ *, */,/g;s/ *$//' file

2. field #12, I need to modify to time format, for example, the following record is "0" to "0:00" or "12" to "12:00"

3. field #24, the value "AUS" - I have another text table (see below), which need to map to full name "Australia"

4. field #25 is the payment currency, if the field is empty, it will look up the text table to put the correct currency

00001,LOTUS,Peter cooking Pty Ltd ,02349,N ,162:39 , 102, 78.426000, 0.000000, 78.426000, 0.000000,0 , 0, 0.000000, 0.000000, 0.000000, 0.000000,162:39 , 102, 78.426000, 0.000000, 78.426000, 0.000000,AUS ,AUD

text table:
AUS Australia AUD
IND India USD
CAN Canada USD
# 2  
Old 03-23-2007
here's a rather crude way ( got to fine tune on your own )
Code:
awk -F"," ' BEGIN { OFS=","
	    lookup["AUS"]="Australia";
	    lookup["Australia"] = "AUD"
	    lookup["IND"] = "India"
	    lookup["India"] = "USD"
	    lookup["CAN"] = "Canada" 
	    lookup["Canada"] = "USD"
	    }
	    {	    gsub(/ ,/ , ",", $0 ) #get rid of blanks 
		    gsub(/, / , ",", $0 )
                    gsub(/ , /, "," , $0)
	    }	  
	    $12 ~ /[0-9]/  { $12 = $12":00" }	    
	    { $24 = lookup[$24]	}
	    $25 == "" { $25 = lookup[$24] }
	    { print $0 }
' file

# 3  
Old 03-23-2007
Quote:
Originally Posted by ghostdog74
here's a rather crude way ( got to fine tune on your own )
Code:
awk -F"," ' BEGIN { OFS=","
	    lookup["AUS"]="Australia";
	    lookup["Australia"] = "AUD"
	    lookup["IND"] = "India"
	    lookup["India"] = "USD"
	    lookup["CAN"] = "Canada" 
	    lookup["Canada"] = "USD"
	    }
	    {	    gsub(/ ,/ , ",", $0 ) #get rid of blanks 
		    gsub(/, / , ",", $0 )
                    gsub(/ , /, "," , $0)
	    }	  
	    $12 ~ /[0-9]/  { $12 = $12":00" }	    
	    { $24 = lookup[$24]	}
	    $25 == "" { $25 = lookup[$24] }
	    { print $0 }
' file

After run your statement, the field #12 showing ":00" which is not my my result. The result should be "0:00". any advise?
# 4  
Old 03-23-2007
my sample input is taken from your example
Code:
# more file
00001,LOTUS,Peter cooking Pty Ltd ,02349,N ,162:39 , 102, 78.426000, 0.000000, 78.426000, 0.000000,0 , 0, 0.00
0000, 0.000000, 0.000000, 0.000000,162:39 , 102, 78.426000, 0.000000, 78.426000, 0.000000,AUS ,AUD

output:
Code:
./test.sh
00001,LOTUS,Peter cooking Pty Ltd,02349,N,162:39,102,78.426000,0.000000,78.426000,0.000000,0:00,0,0.000000,0.000000,0.000000,0.000000,162:39,102,78.426000,0.000000,78.426000,0.000000,Australia,AUD

field 12 is changed (red). I am not sure why yours don't.
# 5  
Old 03-23-2007
Quote:
Originally Posted by ghostdog74
my sample input is taken from your example
Code:
# more file
00001,LOTUS,Peter cooking Pty Ltd ,02349,N ,162:39 , 102, 78.426000, 0.000000, 78.426000, 0.000000,0 , 0, 0.00
0000, 0.000000, 0.000000, 0.000000,162:39 , 102, 78.426000, 0.000000, 78.426000, 0.000000,AUS ,AUD

output:
Code:
./test.sh
00001,LOTUS,Peter cooking Pty Ltd,02349,N,162:39,102,78.426000,0.000000,78.426000,0.000000,0:00,0,0.000000,0.000000,0.000000,0.000000,162:39,102,78.426000,0.000000,78.426000,0.000000,Australia,AUD

field 12 is changed (red). I am not sure why yours don't.
ok, let me to run it again...
also, you lookup statement..a bit difficult for me..because of my table (called currency_table.txt) which contain over 200 value, if I add the looup into the script..it may take a lot of time....any other possible solution for this?
# 6  
Old 03-23-2007
$12 ~ /[0-9]/ { $12 = $12":00" }

the above statement is not work..if I split it to run and try the result
# 7  
Old 03-23-2007
Quote:
Originally Posted by happyv
ok, let me to run it again...
also, you lookup statement..a bit difficult for me..because of my table (called currency_table.txt) which contain over 200 value, if I add the looup into the script..it may take a lot of time....any other possible solution for this?
oh ok...then one way is to read in the currency table from awk and store those values into array. an eg
Code:
awk  'BEGIN { FS=" " ;while ( getline < "currency_table_file" ) lookup[$1] = $2 
                               #other statements...
                    }
           ......#other statement.

then array lookup will contain the lookup table.pls try it out for yourself.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Prepend 0 to a field in a record

Hi All, I have a file like below. In the field 9 I am having 14,014,3,001/009 on the records. I want to convert the field to a three digit value. For example 14 should 014 , 3 should 003 11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;14;3;29=0000;1.25... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

3. Shell Programming and Scripting

awk repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

4. Shell Programming and Scripting

Get last field specific record

i have file A as below contents --------------------------- Use descriptive thread titles when posting. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". For example, do not post questions For example, do not deliminated. output file as below:... (2 Replies)
Discussion started by: ANSHUMAN1983
2 Replies

5. Shell Programming and Scripting

Modify record headers from file

Dear All I was wondering if anybody is able to help me with a script I am struggling with. I have to add comments to the "head-lines" (start with >) from an other file according to the ID tag. Only the head lines should be modified. cat File.txt >H1_A1_A2_A3_A4_ID1_A5 A:B:C:S:E:E:K... (4 Replies)
Discussion started by: loba
4 Replies

6. UNIX for Dummies Questions & Answers

Modify a record in a unix file

I have few records in a file and each file consists of a field for state and country of length 50 characters. Currently it being represented as Austin, Texas(remaining segments upto length 50 are blank) I need to modify this to Austin,Texas(remaining segments upto length 50 are blank) The... (3 Replies)
Discussion started by: jayumon
3 Replies

7. Shell Programming and Scripting

get a field from a record

I have a file as: A,B,C,D,E G,H,I,J,K I need to find if fourth field is blank or has a space and print that line to other file. I tried using awk but am not getting the desired result. Pls help. (6 Replies)
Discussion started by: praveenK_Dudala
6 Replies

8. Shell Programming and Scripting

how to replace field for each record

Hello, I have the following xml formatted file. I would like to get the newnumber field number and replace into customernumber for each record. For example: <XMLFORMAT> <customernumberR11>9</customernumberR11> ... (12 Replies)
Discussion started by: happyv
12 Replies

9. Shell Programming and Scripting

mapping record information

hi, I have a file called a.txt which contain some information (see below), I will like to check the mapping.txt and replace/add some information into a.txt a.txt CLIENT01,AUS,AUD CLIENT03,FRC,USD CLIENT02,JPN, CLIENT05,CHA,USD mapping.txt AUS AUD CHA USD FRC EUR JPN YEN a.txt... (2 Replies)
Discussion started by: happyv
2 Replies
Login or Register to Ask a Question