Adding a field to a file using a conversion table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a field to a file using a conversion table
# 1  
Old 06-01-2011
Adding a field to a file using a conversion table

Hello everyone,

Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table" file that will give the category/type associated with each item.

Example Transaction Log:
Code:
Item     Amount      Item Name     Price
11        1              X                  $1.00
12        1              Y                  $1.00
15        1              W                 $1.00
12        1              Y                  $1.00
13        1              Z                  $1.00
12        1              Y                  $1.00
12        1              Y                  $1.00

Example Conversion Table:
Code:
Item     Category
11        Cat
12        Dog
13        Fish
14        Bird
15        Pig

Desired Output:
Code:
Item     Amount      Item Name     Price    Category
11        1              X                  $1.00    Cat
12        1              Y                  $1.00    Dog
15        1              W                 $1.00    Pig
12        1              Y                  $1.00    Dog
13        1              Z                  $1.00    Fish
12        1              Y                  $1.00    Dog
12        1              Y                  $1.00    Dog


I have been trying to do this with a bash script but I have having some with syntax and executing the commands. My thoguht process is to do it with sed, head, and awk. Is this the most effiecent way?

Code:
script.sh
#!/bin/bash
fname=$1
head -n 2000 $fname | awk -F t '{print $30, $32, $33, $33 - 0.40 ,$28}' > "cache.txt"
while read itemNo Category
do
     sed  -i s/"$itemNo"/"$Category"/g cache.txt > cache.txt
done < "key.txt"


p.s. I pass in the filename as a parameter.

Thanks in advance.

Last edited by radoulov; 06-02-2011 at 05:04 AM.. Reason: Code tags.
# 2  
Old 06-02-2011
Try this,
Code:
awk 'NR==FNR{a[$1]=$2;next} a[$1]{print $0,a[$1]}' ConversionTable TransactionLog

# 3  
Old 06-02-2011
pravin27 I owe you a digital beer. You have saved me many hours of scratching my head. Thanks for quick response. I just inserted the code and changed the file names and Boom! It worked!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replacing string/special characters using a 'conversion' table

Hi, Does anyone know if there is a script or program available out there that uses a conversion table to replace special characters from a file? I am trying to remove some special characters from a file but there are several unprintable/control characters that some I need to remove but some I... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Adding field to file

Hi I have file as given below 040|14300|40.0|563000 042|13200000|40.0 041|100|40.0 043|10000|40.0 045|102|40.0 I want to check if field 4 is null then I have to place | in position 4. result set should be 040|14300|40.0|563000 042|13200000|40.0| 041|100|40.0| 043|10000|40.0|... (9 Replies)
Discussion started by: shabeena
9 Replies

3. Shell Programming and Scripting

XML to Table conversion

<title>00000888</title> <name>XYZ</name> <updated>2397-2323</updated> <content>Content</content> <summary>summary12</summary> <title>00000788</title> <name>ABC</name> <updated>99090-09</updated> <content>Content123</content> ... (4 Replies)
Discussion started by: nanduri
4 Replies

4. Shell Programming and Scripting

Adding an additional blank field to a file

Hi, I have the following file, I'd like to add an additional blank field to this file This is a tab delimited file, I have tried the same thing on excel, but looking for a unix solution. Here is my input: Country Postal Admin4 StreetBaseName StreetType HUN 2243 Kóka Dózsa György ... (3 Replies)
Discussion started by: ramky79
3 Replies

5. UNIX for Dummies Questions & Answers

Find & Replace identifiers using a conversion table

Hi ! I have input.tab with one column containing Item IDs under a number format (the second column is the Location of this item): Location Item ID rack1 12; 35; 43 rack35 23; 894; 5478; 98 etc... (The number of Items per row is variable. Item IDs in a same field are... (17 Replies)
Discussion started by: lucasvs
17 Replies

6. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

7. Shell Programming and Scripting

Delimted to padded conversion with unknown field length

I’m looking for an elegant way to convert a delimited file (comma delimited in this case) to padded columns (for printing in non-proportional font) but the length of each column is not known ahead of time. It needs to be calculated for each column from the longest entry in that column in a given... (3 Replies)
Discussion started by: Michael Stora
3 Replies

8. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

9. Shell Programming and Scripting

Adding field to file and moving the last 2 fields

I have a file with 32 fields each separated by ‘|”. I need to add a file date exactly in the format “ "20100120" “ as the 32nd field moving the existing 32nd field to 33. so the field I added should be 32nd and the 33rd field is the last field before I added the file date. I know we can... (8 Replies)
Discussion started by: dsravan
8 Replies

10. UNIX for Dummies Questions & Answers

Conversion problem with date field and formatting.

Hi, My input file contains the record(s) as below with space as FS. 01-01024180 35000 MV010 02/03/09 0306 03060226 03 02-00410330 470000 MV010 02/03/09 0301 03010276 03 1. I need to convert the field 02/03/09 (dd/mm/yy) to yyyymmdd yet retain the Field separator. Using the modified... (2 Replies)
Discussion started by: talk2pawee
2 Replies
Login or Register to Ask a Question