How to replace word in CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace word in CSV
# 1  
Old 04-24-2008
How to replace word in CSV

Hi,
Can someone please help? I am using Sun OS unix:
1. read a CSV file, port.csv
2. find out all lines which has word "Documentation"..as I only need to change on those lines which has word, "Documentation"
3. then for each found line, I have to read the 9th element, which always has the pattern of CCC-CCC-### e.g. HKH-CSA-123, HKH-CSA-456
4. then I have to get the last 3 digit and try to find the replace ID in another file config.txt e.g. the replace id for 123 is 6.
5. The pattern in config file is
123,6
456,7
6. If I can find the replace ID, then replace column 11 in the CSV file with the replace ID. Currently, it is lucky that column 11 always has the
value of 5
7. Apply the changes to a new file(port2.csv) but not orignal file port file (port.csv)



before in port.csv:
hello, world, my, name, is, aa, bb, cc, HKH-CSA-123, 10, 5, 2008
hello, mom, his, name, is, dd, ee, ff, HKH-CSA-999, 23, 5,2008
hello, dad, my, name, is, aa, bb, cc, HKH-CSA-456, 24, 5,2008


After changes, create a new file port2.csv with following content:
hello, world, my, name, is, aa, bb, cc, HKH-CSA-123, 10, 6, 2008
hello, mom, his, name, is, dd, ee, ff, HKH-CSA-999, 23, 5,2008
hello, dad, my, name, is, aa, bb, cc, HKH-CSA-456, 24, 7,2008


given there are only 2 entries in config file
123,6
456,7

Below is my trial version of shell script. I need help on completing this task:


#!/usr/bin/ksh
echo "*********** Program starts at `date +%Y%m%d\ %H:%M:%S` **************"
FILENAME=port.csv
CONFIG_TXT=cfg.txt
AGREEID=
RESULT=`grep "Documentation" $FILENAME`
for LINE in $RESULT
AGREEID=`cat $LINE | awk -F"," '{print $9}'`
# how get the last 3 char of agree id b?
echo "AGREEID: $AGREEID"
REPLACE_ID=`cat "$CONFIG_TXT" | grep "$AGREEID" | awk -F"," '{print $2}'` # try to retrieve the replace ID given the agree ID in config file
if [[ -z REPLACE_ID && REPLACE_ID=""]] # if agree ID not found in config file, so no replacement can be performed
then
:
else
#dun know how to replace 11th element with REPLACE ID
sed 's/"5"/"$REPLACE_ID"/g' $LINE
#how to apply the changed file to a new file?
fi
exit 0
# 2  
Old 04-24-2008
Try this with awk:

Code:
awk '
BEGIN{FS=OFS=","}
NR==FNR{arr[$1]=$2;next}
{split($9,a,"-")}
a[3] in arr{$11=arr[a[3]]}1' "conf.file" "port.csv"

Regards
# 3  
Old 04-24-2008
Thx for yr reply...as I am a newbie in unix scripts , would u please kindly explain the lines of code? Also, where is the checking of word "Documentation"? Updated is ony performed if the line in the csv fie contains "Documentation"

Last edited by kinmak; 04-24-2008 at 12:06 PM..
# 4  
Old 04-24-2008
The word "Documentation" is not in your sample data.


Jean-Pierre.
# 5  
Old 04-24-2008
Try:

Code:
awk '
BEGIN{FS=OFS=","}
NR==FNR{arr[$1]=$2;next}           # Create an associative array using the fields of the 1st file
/Documentation/{split($9,a,"-")    # Search for lines with pattern and create an array to extract the value in a[3]
if(a[3] in arr){$11=arr[a[3]]}}    # If field 11 is in array a replace value
1' "conf.file" "port.csv"          # Print the line

You should have a read of an awk book or tutorial.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | awk 'gsub(/^|abc,|$/,"") {print}' Required output {grp_key,xyz,aaa,ccc} (5 Replies)
Discussion started by: 100bees
5 Replies

2. Shell Programming and Scripting

Find every alternate word in a csv

Hi, This should be really simple. But not sure why I am unable to crack it. I am looking for a simple one liner to print every alternate word in a csv. say my file is having content : abc,def,ghi,jkl,..... i need the output as below abc ghi (7 Replies)
Discussion started by: ctrld
7 Replies

3. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

4. UNIX for Dummies Questions & Answers

Word Wrap .CSV Fils

Is there a generic method for applying word wrap to all cells for a .csv file? (6 Replies)
Discussion started by: jimmyf
6 Replies

5. Shell Programming and Scripting

Count characters in a csv file and add an word.

Hello, I want to add a sentence to "post column" those who are only less than 30 characters.Thank you very much for your help. "category","title","post" "Z","Zoo","test 54325 test 45363mc." "Z","Zen","rs2w3rsj 2d342dg 2d3s4f23 d23423s23h 2s34s2423g ds232d34 2342." "Z","Zet","test4444... (3 Replies)
Discussion started by: hoo
3 Replies

6. Shell Programming and Scripting

How to replace the word?

Hi Everybody, I am having a query like I want to change only first three patterns of the first line in a file. ex: I like unix unix unix unix is very much. after using the command it should be like I like linux linux linux unix is very much. Thanks, Naga (3 Replies)
Discussion started by: nagraju.allam
3 Replies

7. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

8. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

9. Shell Programming and Scripting

How to replace a word with another word

Hello Friends, How to substitue with a word by another word in file written in VI Editor. Thanks & Regards Siva Ranganath 7760774961 (2 Replies)
Discussion started by: sivaranganath
2 Replies

10. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies
Login or Register to Ask a Question