File names as array element in ksh

Hi, I have an ksh array(ARR). the elements to the array are file names. i need to go to each file in the array and manipulate the records.

for name in ${files[@]}; do   ---this loop is for all the file names in the array
	for i in $(wc -l < $name); do       --this loop is for all the records in one                   
                                                             array element(i.e.file)
	sed 's/^.*Member //; s/Not Found In Database.*$//' <<<
                                        -----after <<< , i need to get each record value 
	done
done

could anyone pls suggest on how to get each record in a file.

1 Like

Perhaps something like this:

for name in ${files[@]}
do
   sed  's/^.*Member //; s/Not Found In Database.*$//' $name |
   while read line
   do
       #  --- Do something with $line in here
   done
done

Thanks Chubler_XL for ur reply. I tried the option u suggested. But its hanging. moreover i want the sed statement inside the while loop. so when i tried the below, it didnt work.
Pls note that, name is a file name. while loop should loop for all records inside the file.

for name in ${files[@]}
do
   while read line
   do
   sed  's/^.*Member //; s/Not Found In Database.*$//' $line |
   done
done

No, the for loop content needs to leverage '$name'.

Either 'sed' or 'while read <vars>' can read a file, but it is wasteful to call sed for every line.

If the file name is not part of output, you can "sed '<script>' <list_of_files> | . . ." so one sed does all. If you can use sed (or awk) to manipulate, you are done. What sort of manipulate do you want?

Hi DGPickett,
I wanted to extract one string from all the records of the file. I am able to achieve that now. Now i can write the values to array. now i need to send this by mail. Is it possible to send array value in mail body or i need to write the array elements to a file n then send the file as attachment. Pls suggest.

A popular file to send, inside email or as an attachment, is CSV (comma separated value). It reads into Excel, Access and such. It supports a 2 dimensional table or matrix, with commas separating columns and cr-lf separating rows (but either is usually sufficient). "If a cell contains ',', it needs to be double quoted." "If a cell has a double quote, it needs to be doubled ""." Name it whatever.csv and away you go. If you are " and , free, it is very simple.

Hi DGPickett, Now i am able to write the array to a file and send the file as attachment in mail. I have one column in my file coz i am writing one dimensional array to file. i know we cant make multi-dimensional array in kxh. is there any other way we can write 2 columns in the file.
now my file contains:

a
b
c

can i achieve the below in my file?

a a.txt
b b.txt
c c.txt

Try:

#!/bin/ksh
arr=(a b c)
for i in "${arr[@]}"
do      printf '%s %s\n' "$i" "${i}.txt"
done > file
1 Like

Thanks Don. But my requirement was
a,b,c are members i found from a set of files say 1.txt, 2.txt, 3.txt respectively. so i am storing a,b,c in array and showing tht in file. i want my array/file to be like below

a(the member), 1.txt(the file from where the member 'a' was found)
b(the member) 2.txt(the file from where the member 'b' was found)
like this.

Apologies for the confusion in my previous post.

Why not create two arrays member and file

#!/bin/ksh
member=(a b c)
file=(1.txt 2.txt 3.txt)

i=0
while [ $i -lt ${#member[@]} ]
do
   printf "%s %s\n" "${member}" "${file}"
   let i=i+1
done
1 Like

Thanks much Chubler_XL. I was able to get the desired result.Now i am trying to put a header to the output file and also insert a new line character after each record like below. I can see the same achieved in the file generated . but when i am sending that mail in mail attachment, i see theres no new line and header also coming in the same line.

Getting the below in generated file

member      file
a                 1.txt
b                 2.txt
c                  3.txt

getting the below as mail attachment

member      file a     1.txt b 2.txt c 3.txt

This could be and outlook mail client issue. Try putting a space (or two) at the front of each line:

printf "  Member    File\n   \n"
...
printf "  %s %s\n  \n" "${member}" "${file}"

Hi Chubler_XL, I am trying the below code, but this doesnt seem to work.

echo "  %s %s\n  \n" "${File}"  "${Member}" >> temp.txt

Try printf not echo

Hi Chubler_XL, i tried with printf also..its not working.
i tried the below:

printf "%s %s  \n  \n " "${File}"  "${Member}" >> temp.txt

By not working what do you mean is the data all still on one line?

Are you inserting the contents of temp.txt into the email body, or attaching it to the email, I'm assuming your inserting it into the email body.

Check the contents of temp.txt what you want is two spaces in the front of every line in this file that you want starting on a new line. Outlook thinks any line without spaces in the front is part of the previous paragraph and joins it up.

Without code tags around you posted code it's a little hard to tell what use used but it looks like you dont have the 2 spaces at the front on the printf string. You need the printf to be like this:

printf "  %s %s\n  \n" "${File}" "${Member}" >> temp.txt
1 Like

Hi Chubler_XL, Yes the data still comes in one line. I am attaching the file in the mail, its not body.
below is the code.

printf "  %s %s\n  \n " "${File}"  "${Member}"  >> temp.txt
cat temp.txt |sort|uniq|sed "1i File Names  \t \t Rejected Member Names\n" > rejmbrs.txt

if i check in Unix, the values are appearing in separate lines as per requirement in rejmbrs.txt.
But when i am attaching the rejmbrs.txt in my email, values are coming in one line.

OK that explains things.

Files in Windows/DOS are different to unix, they need \r\n on the end. Change your printf commands to put \r infront of any \n eg:

printf "%s %s\r\n" "${File}"  "${Member}"  >> temp.txt

Another option is to use the unix2dos program on your file before attaching:

unix2dos rejmbrx.txt
1 Like

Hi Chubler_XL, Thanks a ton. You are the Unix Master. I am able to get desired result with your suggestion. Thanks again.:b:

In CSV:

a,a.txt\r
b,b.txt\r
c,c.txt\r

Tab separated text is also well supported, without worries about ',' and '"'. But CSV is more robust, allowing you to put multi-line chunks into cells inside ":

John,Smith,"123 Main ST
Anytown, NJ 08088-121",Democrat\r
Sam,Jones,"234 2nd AVE
Othertown, PA 18088-3456",Republican\r
1 Like