Add commas to end of each line (but not the last)

I am reading a file and adding a comma after each line. The hurdle I’m stuck on is not adding the comma to the last line. Any ideas?

((concatenation "," of substrings separated by "%0d%0a" of it) & ",") of ( lines of file "foo.bar" of storage folder of client )

There may be better ways of doing this, but how about something like:

(if (line number of it < (number of lines of file "foo.bar" of storage folder of client)) then it & "," else it) of (lines of file "foo.bar" of storage folder of client)

Yeah, that is what I was afraid of. Processing time went from 4ms to 150ms. But it does solve the problem.

How about just sticking one on the end?

((concatenation "," of substrings separated by "%0d%0a" of it) & ",") of ( lines of file "foo.bar" of storage folder of client ) & ","

Or did I misread that? You want to have a comma at the end and not getting one, or getting a comma at the end that you don’t want?

If you don’t want the trailing comma, how about preceding text of last "," of (concatenation ...

That’s a great idea @JasonWalker. I’m looking to remove just the final comma. But when I run it through the relevance I only see the last line. If I concatenate it a second time it looks like it will work, but it pulls all the fields into one big string (no returns/new line).

The final question… how would I put the returns back in? I tried putting in “%0d%0a” at the outer concatenation, but that didn’t work. Any ideas? Otherwise this appears to get us to 99% and that’s pretty good. Thank you both!

preceding text of last "," of (concatenation "" of ((concatenation "," of substrings separated by "%0d%0a" of it) & ",") of ( lines of file "foo.bar" of storage folder of client ))

Ok I had to read that a couple of times. You shouldn’t need substrings separated by "%0d%0a" because “lines of file” should do that for you already. And no need to append a final “,” if you want to trim that back out anyway. I think parentheses scopingmay be messing it up a bit. Give this a try…

concatenation "," of lines of file (whatever)

In your first try, you were operating separately on each line.

Yea, that still grabs just the last line.

I think I’m missing something here. Can you post an example input/output? Here’s what I get…

q: lines of file "c:\temp\test.txt"
A: This is line 1
A: This is line 2
A: This is line 3
T: 0.333 ms
I: plural file line

q: concatenation "," of lines of file "c:\temp\test.txt"
A: This is line 1,This is line 2,This is line 3
T: 0.562 ms
I: singular string

I think I see why. The field is actually a single field with multiple returns, which is different from a plural return. We create a file with this command…

concatenation "%0d%0a" of mac addresses of adapters of network

I’ll have to take a look back and see why we did it that way instead of just doing the plural returns of …

mac addresses of adapters of network

which would work far better. We are doing some background work with the data, but getting rid of the extra concatenation may be the better solution.

I think i was a noob when I did this initially and this is how I got over whatever hurdle I initially hit.

In an appendfile/createfile command, you’d need the concatenation “%0d%0a” to get each result on a separate line.

But using the “lines of file” inspector to read the file should already be splitting into multiple results based on the “%0d%0a” - that’s the Carriage Return / Line Feed pair of characters used as the end-of-line mark on DOS files.

Is this on a Mac/Linux host? If so they just use the newline character which I think is %0a, not both %0d and %0a.

This is for all OSs, Win/Mac/Lin.

Thinking the solution I got is good enough. I pipe the data through a python script that does a bit more manipulation before calling it done. The extra returns here should be good enough to make this act like the other splits and merges I am doing.

Thank you so much for the help!