9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] How do you deal with newlines (line feed, LF, \n) in filenames?
@ 2024-08-10 20:46 kalterdev
  2024-08-10 21:05 ` sirjofri
  2024-08-10 21:32 ` [9fans] " kalterdev
  0 siblings, 2 replies; 4+ messages in thread
From: kalterdev @ 2024-08-10 20:46 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 802 bytes --]

Suppose you want to grep over a filtered list of files. Is there an idiomatic approach to solve this problem with rc?

I can only think of one erroneous method:

grep pattern `{find . -type f -name '*_test.go'}

I know that it's possible to use find(1)'s -exec option. But find does not exist in Plan 9.

It seems like it is impossible to solve this problem with simple IO pipes. One must encode newlines somehow, be it C-style \n, shell-style 'string', JSON or whatnot.

It also seems that newlines are the only symbol that breaks the idiom `grep pattern `{cmd}`.
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T11149f1f949593f5-M80e746fe9ffc98e7d9511774
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 1464 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9fans] How do you deal with newlines (line feed, LF, \n) in filenames?
  2024-08-10 20:46 [9fans] How do you deal with newlines (line feed, LF, \n) in filenames? kalterdev
@ 2024-08-10 21:05 ` sirjofri
  2024-08-10 21:32 ` [9fans] " kalterdev
  1 sibling, 0 replies; 4+ messages in thread
From: sirjofri @ 2024-08-10 21:05 UTC (permalink / raw)
  To: 9fans

Hi,

having newlines in filenames is questionable. However, needing a newline is quite common. In rc, you don't encode the newline, you just use it:

echo here is my '
' newline.

It's quite common to define it as a variable for easier use, for example:

nl='
'
echo $nl
files=`$nl{ls}
echo $files(1)

(The example is not that useful, because the standard delimiters contain a newline character.)

Instead of find, I usually use walk, but I don't really know the syntax. In these rare situations I often find myself using walk | grep instead of looking up the flags.

Good luck finding a solution that works for you.

I think there was a doscussion some time ago to add quotes to ls output for situations like these.

sirjofri

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T11149f1f949593f5-M4a923f98af111ae0821055d2
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [9fans] Re: How do you deal with newlines (line feed, LF, \n) in filenames?
  2024-08-10 20:46 [9fans] How do you deal with newlines (line feed, LF, \n) in filenames? kalterdev
  2024-08-10 21:05 ` sirjofri
@ 2024-08-10 21:32 ` kalterdev
  2024-08-10 21:48   ` sirjofri
  1 sibling, 1 reply; 4+ messages in thread
From: kalterdev @ 2024-08-10 21:32 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 885 bytes --]

Hi sirjofri,

Thank you for your reply. I was referring to having newlines specifically in filenames. Sometimes it's not one's own fault that filenames have them.

I wasn't really speaking about the way to represent a newline in rc, although thanks for details on that problem. My problem is to parse command output that has a newline in a filename.

On my system, find(1) just prints the newline as is. So if the output looks like this:

a
b

It is actually possible that it means one "a\n" file, not file "a" and file "b".

Some commands, including ls(1) from plan9port, support quoting such files. But then the problem is how to parse them.
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T11149f1f949593f5-M2e88845b92e7a51dca615ea9
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 1592 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9fans] Re: How do you deal with newlines (line feed, LF, \n) in filenames?
  2024-08-10 21:32 ` [9fans] " kalterdev
@ 2024-08-10 21:48   ` sirjofri
  0 siblings, 0 replies; 4+ messages in thread
From: sirjofri @ 2024-08-10 21:48 UTC (permalink / raw)
  To: 9fans

10.08.2024 23:32:48 kalterdev@gmail.com:
> Thank you for your reply. I was referring to having newlines specifically in filenames. Sometimes it's not one's own fault that filenames have them.

Sure, that can happen. I just wonder who would do that on any system.

> I wasn't really speaking about the way to represent a newline in rc, although thanks for details on that problem. My problem is to parse command output that has a newline in a filename.
>
> On my system, find(1) just prints the newline as is. So if the output looks like this:
>
> a
> b
>
> It is actually possible that it means one "a\n" file, not file "a" and file "b".

I think in this case it would be either two files ("a" and "b") or one file ("a\nb") because otherwise it would look like this:

a

b

> Some commands, including ls(1) from plan9port, support quoting such files. But then the problem is how to parse them.

Yeah, that's more complicated then. I don't think we have standard tools to deal with that situation out of the box yet.

That is, if you're lucky enough to have an ls, walk or find that can quote them, you can parse them using different tools (awk, sed, or build them yourself in C). It would probably easier to circumvent this by getting file by file, for example using some dirstat counterpart or maybe even a for loop (but I think for loops also work with command output).

Maybe someone else can help you find a more pleasing solution, sorry.

sirjofri

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T11149f1f949593f5-M57ab58e7079320469579f89b
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-08-10 21:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-10 20:46 [9fans] How do you deal with newlines (line feed, LF, \n) in filenames? kalterdev
2024-08-10 21:05 ` sirjofri
2024-08-10 21:32 ` [9fans] " kalterdev
2024-08-10 21:48   ` sirjofri

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).