Sponsored Content
Full Discussion: How to replace word in CSV
Top Forums Shell Programming and Scripting How to replace word in CSV Post 302188693 by kinmak on Thursday 24th of April 2008 04:26:56 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
csa_logon(library call) 												   csa_logon(library call)

NAME
csa_logon -- log on to the calendar service and establish a session with a calendar SYNOPSIS
#include <xcsa.h> CSA_return_code csa_logon( CSA_service_reference calendar_service, CSA_calendar_user *user, CSA_string password, CSA_string character_set, CSA_string required_csa_version, CSA_session_handle *session, CSA_extension *logon_extensions); DESCRIPTION
The csa_logon function allows the calling application to logon to the calendar service. If the specified calendar does not exist, then the error CSA_E_CALENDAR_NOT_EXIST is returned. The function returns a Session Handle that the application will use in subsequent CSA calls. ARGUMENTS
Calendar Service (Service Reference) A calendar service name specifying the requested calendaring service, (e.g., the path to a calendar store or a remote server node name). This value may be NULL if the underlying calendaring service does not require a service name or if UI is allowed. This may be necessary on some implementations and ignored on others. User (Calendar User) A pointer to the calendar user structure that identifies the user and calendar to the calendaring service. This value may be NULL. Password (String) A string containing the password required for access to the CSA service. This value may be NULL if the underlying calendaring service does not require a password or if UI is allowed. Character Set (String) A CSA formal public identifier string for the character set of strings used by the CSA caller. The client may pass NULL in which case the character set used is determined by the CSA service. The supported values are implementation specific. Required CSA Version (String) The formal public identifier for the CSA version number required by the application. For this version of the specification this string must be ``-//XAPIA/CSA/VERSION1/NONSGML CSA Version 1//EN''. Logon Extensions (Extensions) A pointer to an array of CSA_extension structures for this function. The array may contain both input extensions for providing additional information to the function and output extensions for receiving information from the function. A value of NULL indicates that the caller is not using any extensions. See the extensions structure for more information. Through extensions, the application can find out which extensions are available and set which data extensions will be active for the ses- sion. RETURN VALUE
Session (Session Handle) Opaque session handle that represents a session with the CSA calendar. Logon Extensions (Extensions) If output extensions were passed to the function in the extensions list, the results from the service will be available in the extension. See the extensions structure for more information. Whether the function succeeded or not, and, if not, why. It may be success or one of the values listed under ERRORS below. ERRORS
The csa_logon function returns the following error values: CSA_E_CALENDAR_NOT_EXIST The specified calendar does not exist. CSA_E_FAILURE There was a general failure that does not fit the description of any other error code. CSA_E_INSUFFICIENT_MEMORY Insufficient memory was available to complete the requested operation. CSA_E_INVALID_CALENDAR_SERVICE The underlying calendar service is invalid, so logging on cannot be completed. CSA_E_INVALID_CONFIGURATION The specified logon configuration is inconsistent with the selected calendar service. CSA_E_INVALID_DATA_EXT The data extension requested is invalid. CSA_E_INVALID_FLAG A flag value in the flags argument was invalid. CSA_E_INVALID_FUNCTION_EXT The function extension requested is invalid. CSA_E_INVALID_PARAMETER A function parameter was invalid. CSA_E_INVALID_PASSWORD The password is incorrect. CSA_E_INVALID_USER The specified calendar user is invalid. CSA_E_NO_AUTHORITY The user has insufficient authority for this function. CSA_E_PASSWORD_REQUIRED A password is required on this calendar service. CSA_E_SERVICE_UNAVAILABLE The requested calendar service is unavailable. CSA_E_TOO_MANY_USERS The implementation cannot support the additional logon of another calendar user at this time. CSA_E_UNSUPPORTED_CHARACTER_SET The character set requested is not supported. CSA_E_UNSUPPORTED_DATA_EXT The data extension requested is not supported. CSA_E_UNSUPPORTED_FUNCTION_EXT The specified function extension is not supported or CSA_EXT_REQUIRED is set. CSA_E_UNSUPPORTED_VERSION The specification version specified in the call cannot be supported by this CSA implementation. SEE ALSO
csa/csa.h - csacsa(5), csa_add_calendar(3), csa_add_entry(3), csa_call_callbacks(3), csa_delete_calendar(3), csa_delete_entry(3), csa_free(3), csa_free_time_search(3), csa_list_calendar_attributes(3), csa_list_calendars(3), csa_list_entries(3), csa_list_entry_attributes(3), csa_list_entry_sequence(3), csa_logoff(3), csa_look_up(3), csa_query_configuration(3), csa_read_calen- dar_attributes(3), csa_read_entry_attributes(3), csa_read_next_reminder(3), csa_register_callback(3), csa_unregister_callback(3), csa_update_calendar_attributes(3), csa_update_entry_attributes(3). csa_logon(library call)
All times are GMT -4. The time now is 02:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy