array in ksh with elems containing spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting array in ksh with elems containing spaces
# 1  
Old 05-27-2008
array in ksh with elems containing spaces

Hi all,

I've a .csv file containing data per line delimited with '|' (The fields may contains elements with spaces).

e.g. (really a sample)
Code:
ID|Name|fon
12345|Celal Dikici|+4921123456
12346|Celal Dikici Jun.|+4921123456
12347|Celal Dikici Sen.|+4921123456
12348|Celal|+4921123456
a.s.f..20000 lines of data...

In my ksh script I use
Code:
cat ${KUSY_LocalFile} | grep -v "^#" | grep -v "^$" | while read KUSYLINE
do
			
  # Variablennamen aus Satzaufbbau
  GP_PARTNER=`echo ${KUSYLINE} | nawk -F"|" '{print $1}'`
  ....a.s.f....until 11 declarations
  GP_MC_NAME2_RG=`echo ${KUSYLINE} | nawk -F"|" '{print $11}'`
  ...
  SQLStatement="INSERT INTO customdb..ZW_GPDaten VALUES( \"${GP_PARTNER}\",  ....a.s.f.... )"

  # Jetzt SQL-Statement ins File und Info
  echo "${SQLStatement}" 		>> ${KUSY_SQLFile}
done
  ...isql < ${KUSY_SQLFile}

This works, but not fine. It tooks about 1h to generate the sql code.
I assume the nawk in every line of the 11 variable delarations is the source for this behaviour.

I'm now looking for faster working solution. In perl I would do something like

Code:
( $var1, ..$var12 ) = split( "|", $LINE );

I read that awk has equal to perl a split, too (I'm not so familar with awk except
simple usage like above).

So the idea was to pass the ${KUSYLINE} to awk and get
the results into a ksh array.

like
Code:
echo ${KUSYLINE} | set -A NAWKARRAY `nawk -F"|" '{ split( $0, partner, "|")
				print partner[1] partner[2] partner[3] partner[4] partner[5] partner[6]
				partner[7] partner[8] partner[9] partner[10] partner[11]}'`

...
GP_PARTNER=${NAWKARRAY[0]}
...11 declarations...
GP_MC_NAME2_RG=${NAWKARRAY[10]}

The idea works generally (see sample code in the file attached to this message).

But the set -A with spaces in the occurs into shifting of the vars.
I didn't get e.g. for GP_PARTNERX=${NAWKARRAY[y] -> Celal Dikici; I get GP_PARTNERX = Celal and GP_Number=Dikici.

What is the optimal way? Or can set -A used with different delimiters?

Can someone help me??

Regards,
Celal
# 2  
Old 05-27-2008
You can try something like:

Code:
nawk -F"|" -v file=${KUSY_SQLFile} '!/^#/ && NF {
print "INSERT INTO customdb..ZW_GPDaten VALUES(" $1 ",....  )" >> file
...
...
}' ${KUSY_LocalFile}

Regards

Last edited by Franklin52; 05-27-2008 at 06:23 PM.. Reason: adding fieldseperator & typo
# 3  
Old 05-27-2008
You do not need to use awk to parse each line. You can set the IFS to ';'
and simply read each line into an array (i.e. myarray) as shown in the following
code snippet.

Code:
#!/usr/bin/ksh93

IFS=';'
while read -A myarray 
do
    print ${myarray[*]}
done < file

To reduce run time you need to use shell builtins as much as possible and not
call external programs such as awk.
# 4  
Old 05-28-2008
Quote:
Originally Posted by fpmurphy
To reduce run time you need to use shell builtins as much as possible and not call external programs such as awk.
Even if ksh is *much* faster then bash for this type of parsing I often found that awk was faster than pure shell solutions in reading and processing lines. To make sure, I just ran a benchmark on a sample file with 20000 lines using Franklin52's awk snippet and the following ksh93 code:

Code:
IFS='|'
while read -A myarray 
do
        print "INSERT INTO customdb..ZW_GPDaten VALUES(${myarray[0]}, ${myarray[1]}, ${myarray[2]} )" 
done < sample_file

Results:
awk: 0.146 sec.
ksh: 0.636 sec.

So, awk is five times faster albeit being an external program. And, just for the record, I also tested a bash version and, as expected, it's the slowest:

bash: 2.935 sec.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop through array of arrays of string with spaces

Hi I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of... (4 Replies)
Discussion started by: kidmanos
4 Replies

2. Shell Programming and Scripting

How to handle variable with spaces in ksh

I'm having issue capturing a value from file.list with a multiple spaces in a variable $i, tried various options like using double quotes, no quotes, single quotes, curly braces but to no avail. cat file.list aaa test bbb ccc test ddd eee test fff for i in `cat file.list` do echo "$i";... (2 Replies)
Discussion started by: mbak
2 Replies

3. Shell Programming and Scripting

spaces in array field

I have a file, names(i) where each entry is 'first last' name. 'cat names' is fine. But in a shell script >for file in $(cat names) > do > echo $file > done the first and last name appear on 2 lines. I have tried escaping and quoting the space but to no avail. The names are to be... (4 Replies)
Discussion started by: chuckmg
4 Replies

4. Shell Programming and Scripting

ksh: removing all white spaces

'String' file contains the following contents, D11, D31, D92, D29, D24, using ksh, I want to remove all white spaces between characters no matter how long the string is. Would you please give me some help? (1 Reply)
Discussion started by: yoonius
1 Replies

5. Programming

Concatenating array of strings into one string separated by spaces

Hi, Well as the title says, I have an array of strings (delimited by null). The length of the array is variable and length of each string is variable as well. What I need is one huge string with the original strings in the array separated by spaces. For example is an array is such that array... (12 Replies)
Discussion started by: newhere
12 Replies

6. Shell Programming and Scripting

ksh - read file with leading spaces

Hi, Could someone has any suggestions on this? When read a line from a file, I need to check the first char in the line, it could be a space or any char. But the leading spaces are removed by read. Thanks. (2 Replies)
Discussion started by: momi
2 Replies

7. Shell Programming and Scripting

ksh: Comparing strings that contain spaces and working with substrings

Forgive me. I am very new to kornshell scripts. The simplest things stop me dead in my tracks. Here are two such examples. I want to save the first 19 characters of the following string to a variable. "Operation Completed and blah blah blah" I know this works (from another thread): ... (2 Replies)
Discussion started by: nancylt723
2 Replies

8. UNIX for Dummies Questions & Answers

problem with the blank spaces in array

Hi all! i need your help. I'm getting started with this... in need to insert in an array string values. But the thing is that this strings have blank spaces... for example if a want to put "filed1 = " and "field2 = " .... in the array , when i want to print all the fields, it only shows... (4 Replies)
Discussion started by: kamicasse
4 Replies

9. Shell Programming and Scripting

sh, ksh: command to remove front spaces from a string?

dear pro-coders, is there any command out there that takes out the front spaces from a string? sample strings: 4 members 5 members 3 members but it has to be like so: 4 members 5 members 3 members (3 Replies)
Discussion started by: pseudocoder
3 Replies

10. Shell Programming and Scripting

How to get array to not split at spaces?

I have been working on some code for a while, that will parse a log file, look for a specified time discrepancy between entries, and then print that line +/- n other lines out to a file... #!/bin/bash file=$1 # The input log file maxTime=$2 # The time discrepancy to look for n=$3 ... (1 Reply)
Discussion started by: jjinno
1 Replies
Login or Register to Ask a Question