9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Help with a sam cleanup script
@ 2021-07-21  9:17 revcomninos
  2021-07-21  9:24 ` Rodrigo G. López
                   ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: revcomninos @ 2021-07-21  9:17 UTC (permalink / raw)
  To: 9fans

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

In ed I have a cleanup script which I can apply to several files at once. The script removes things like blank lines and spaces between words. To run it on a file or multiple files, I type this command:
ed [name of file(s)] < cleanup-script
I converted all the regular expressions in the file into sam regex format and tried the same, but to no avail.  Here is a list of some of the commands:
.x s/\*//g
,x/^ +/s///
,x/ +$/s///
,x/  +/s// /
,x/^$\n/d
,x/‘‘|’’/s//"/
,x/“|”/s//"/
,x/‘|’/s//"/
,x/^ +/s///
,x/ +$/s///
,x/  +/s// /
,x/^$\n/d
,x/‘‘|’’/s//"/
,x/“|”/s//"/
,x/‘|’/s//"/
,x/teh/c/the/
wq
I am not even sure sam can do this, but if it can, I would appreciate some guidance on how to do it. 


------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M81420cb8a514930c24cd6ad6
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21  9:17 [9fans] Help with a sam cleanup script revcomninos
@ 2021-07-21  9:24 ` Rodrigo G. López
  2021-07-21  9:24 ` Rob Pike
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 15+ messages in thread
From: Rodrigo G. López @ 2021-07-21  9:24 UTC (permalink / raw)
  To: 9fans

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

try with ssam(1).

% ssam -f thatscriptofyours somefile


-rodri

On Wed, Jul 21, 2021, 11:17 AM <revcomninos@gmail.com> wrote:

> In ed I have a cleanup script which I can apply to several files at once.
> The script removes things like blank lines and spaces between words. To run
> it on a file or multiple files, I type this command:
> ed [name of file(s)] < cleanup-script
> I converted all the regular expressions in the file into sam regex format
> and tried the same, but to no avail.  Here is a list of some of the
> commands:
>
> .x s/\*//g
> ,x/^ +/s///
> ,x/ +$/s///
> ,x/  +/s// /
> ,x/^$\n/d
> ,x/‘‘|’’/s//"/
> ,x/“|”/s//"/
> ,x/‘|’/s//"/
> ,x/^ +/s///
> ,x/ +$/s///
> ,x/  +/s// /
> ,x/^$\n/d
> ,x/‘‘|’’/s//"/
> ,x/“|”/s//"/
> ,x/‘|’/s//"/
> ,x/teh/c/the/
> wq
>
> I am not even sure sam can do this, but if it can, I would appreciate
> some guidance on how to do it.
>
>
> *9fans <https://9fans.topicbox.com/latest>* / 9fans / see discussions
> <https://9fans.topicbox.com/groups/9fans> + participants
> <https://9fans.topicbox.com/groups/9fans/members> + delivery options
> <https://9fans.topicbox.com/groups/9fans/subscription> Permalink
> <https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M81420cb8a514930c24cd6ad6>
>

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M346a55911fc933521d3d3f07
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21  9:17 [9fans] Help with a sam cleanup script revcomninos
  2021-07-21  9:24 ` Rodrigo G. López
@ 2021-07-21  9:24 ` Rob Pike
  2021-07-21  9:28 ` umbraticus
  2021-07-21  9:52 ` Alexander Kapshuk
  3 siblings, 0 replies; 15+ messages in thread
From: Rob Pike @ 2021-07-21  9:24 UTC (permalink / raw)
  To: 9fans

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

There are many things here that feel wrong or at least inelegant, starting
with the first character. Surely you mean a comma not a period. You are
also inconsistent: sometimes you use x to find the pattern and then change
it, sometimes you use a substitute. It's more efficient in both time and
keystrokes to use c or d when you can:

,x/‘‘|’’/c/"/

,x/\*/d


for example.

But overall: If you show us either the ed script you use, or a precise
definition of the problem, people could offer solutions. I assure you sam
can do this; it's far more powerful than ed.

But then: Why not use ed if you have a working script? Or are you just
trying to understand sam better? The docs for sam are complete. Lose your
line orientation and think of the file as a single string. That's how sam
works.

-rob




On Wed, Jul 21, 2021 at 7:18 PM <revcomninos@gmail.com> wrote:

> In ed I have a cleanup script which I can apply to several files at once.
> The script removes things like blank lines and spaces between words. To run
> it on a file or multiple files, I type this command:
> ed [name of file(s)] < cleanup-script
> I converted all the regular expressions in the file into sam regex format
> and tried the same, but to no avail.  Here is a list of some of the
> commands:
>
> .x s/\*//g
> ,x/^ +/s///
> ,x/ +$/s///
> ,x/  +/s// /
> ,x/^$\n/d
> ,x/‘‘|’’/s//"/
> ,x/“|”/s//"/
> ,x/‘|’/s//"/
> ,x/^ +/s///
> ,x/ +$/s///
> ,x/  +/s// /
> ,x/^$\n/d
> ,x/‘‘|’’/s//"/
> ,x/“|”/s//"/
> ,x/‘|’/s//"/
> ,x/teh/c/the/
> wq
>
> I am not even sure sam can do this, but if it can, I would appreciate
> some guidance on how to do it.
>
>
> *9fans <https://9fans.topicbox.com/latest>* / 9fans / see discussions
> <https://9fans.topicbox.com/groups/9fans> + participants
> <https://9fans.topicbox.com/groups/9fans/members> + delivery options
> <https://9fans.topicbox.com/groups/9fans/subscription> Permalink
> <https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M81420cb8a514930c24cd6ad6>
>

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-Mcecc24b77fbe2df7b744e4f9
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21  9:17 [9fans] Help with a sam cleanup script revcomninos
  2021-07-21  9:24 ` Rodrigo G. López
  2021-07-21  9:24 ` Rob Pike
@ 2021-07-21  9:28 ` umbraticus
  2021-07-21 10:02   ` revcomninos
  2021-07-21  9:52 ` Alexander Kapshuk
  3 siblings, 1 reply; 15+ messages in thread
From: umbraticus @ 2021-07-21  9:28 UTC (permalink / raw)
  To: 9fans

you probably want to use ssam (no wq required)

what isn't working? some suggestions:

,x/‘‘|’’/s//"/ and ,x/‘|’/s//"/ → ,x/[“‘’”]/c/"
.x s/\*//g → ,x/\*/d
,x/^ +/s/// → ,x/^ +/d
,x/ +$/s/// → ,x/ +$/d
,x/  +/s// / → ,x/  +/c/ /

take your time, learn the language, & enjoy

umbraticus

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M7713c653ed3bb3ebbe17be73
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21  9:17 [9fans] Help with a sam cleanup script revcomninos
                   ` (2 preceding siblings ...)
  2021-07-21  9:28 ` umbraticus
@ 2021-07-21  9:52 ` Alexander Kapshuk
  2021-07-21 10:09   ` umbraticus
  3 siblings, 1 reply; 15+ messages in thread
From: Alexander Kapshuk @ 2021-07-21  9:52 UTC (permalink / raw)
  To: 9fans

On Wed, Jul 21, 2021 at 12:18 PM <revcomninos@gmail.com> wrote:
>
> In ed I have a cleanup script which I can apply to several files at once. The script removes things like blank lines and spaces between words. To run it on a file or multiple files, I type this command:
> ed [name of file(s)] < cleanup-script
> I converted all the regular expressions in the file into sam regex format and tried the same, but to no avail.  Here is a list of some of the commands:
>
> .x s/\*//g
> ,x/^ +/s///
> ,x/ +$/s///
> ,x/  +/s// /
> ,x/^$\n/d
> ,x/‘‘|’’/s//"/
> ,x/“|”/s//"/
> ,x/‘|’/s//"/
> ,x/^ +/s///
> ,x/ +$/s///
> ,x/  +/s// /
> ,x/^$\n/d
> ,x/‘‘|’’/s//"/
> ,x/“|”/s//"/
> ,x/‘|’/s//"/
> ,x/teh/c/the/
> wq
>
> I am not even sure sam can do this, but if it can, I would appreciate some guidance on how to do it.
>
>
> 9fans / 9fans / see discussions + participants + delivery options Permalink

As far as having sam commands apply to multiple files, the X command is there:
sam(1):
X/regexp/ command
               For each file whose menu entry matches the regular
expression, make that the current file and run the command.
               If the expression is omitted, the command is run in every file.

Given these target files:
grep . file[12]
file1:line1
file2:line2

And a file that contains some sam commands:
cat cmd
X ,p
q

The following output is printed:
sam -d file[12] <cmd
 -. file1
line1
 -  file2
line2

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-Mbddcbb3fb3e41229d32d0ca7
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21  9:28 ` umbraticus
@ 2021-07-21 10:02   ` revcomninos
  2021-07-21 10:19     ` umbraticus
  0 siblings, 1 reply; 15+ messages in thread
From: revcomninos @ 2021-07-21 10:02 UTC (permalink / raw)
  To: 9fans

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

Thank you all for your responses. I will carefully study the suggestions  provided. sam is new to me and I am trying hard to learn it.  There are obvious errors in the script I provided and I apologise for those. I could as Rob suggested simply apply the ed script, but I am trying to learn sam's way of doing things. 

As for

% ssam -f thatscriptofyours somefile

It gave me this error:

bash: fg: %: no such job

Here is the ed script :
g/\*/s///g
# g/[0-9]/s///g
g/  *$/s///
g/   */s// /g
g/\(‘‘\|’’\)/s//"/g
g/\(“\|”\)/s//"/g
g/\(‘\|’\)/s//'/g
g/\\p/s//\
\
\
/gp
g/\\b/s//\
\
\
/gp
g/^  */s///
v/./.,/./-1j
# Replace two or more blank lines with a single blank line in Ed
wq



------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M20a4b0b4cf349f08a5bc0e9d
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21  9:52 ` Alexander Kapshuk
@ 2021-07-21 10:09   ` umbraticus
  2021-07-26 14:17     ` revcomninos
  0 siblings, 1 reply; 15+ messages in thread
From: umbraticus @ 2021-07-21 10:09 UTC (permalink / raw)
  To: 9fans

also worth mentioning the ^ command if you're using 9front's sam.
put your sam commands in script like this:

#!/bin/rc
cat << STOP
x/  +/c/ 
x/\*/d
STOP

then you can run ^script and it will apply the commands.
Note that if you leave off the commas before the commands,
as I have above, you will have to set dot before running ^
but this allows more flexibility, eg. you can apply the edits
to a section of a document instead of always the whole thing.

One step at a time, though.

It would be neat if you could use ! ^ < > _ | in conjunction with X…

umbraticus

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M2e7ac6c9ebf53e2d3cd2bfec
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21 10:02   ` revcomninos
@ 2021-07-21 10:19     ` umbraticus
  2021-07-21 10:49       ` revcomninos
  0 siblings, 1 reply; 15+ messages in thread
From: umbraticus @ 2021-07-21 10:19 UTC (permalink / raw)
  To: 9fans

> bash: fg: %: no such job

The % is a prompt: ignore it.

I think your script boils down to something like:

,x/\*|^ +| +$/d
,x/  +//c/ 
,x/[‘‘’’“”‘’]/c/"
,x/\\p|\\b/c/\n\n\n
,x/\n\n+/c/\n

umbraticus

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M75c05feaef122451a0351d21
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21 10:19     ` umbraticus
@ 2021-07-21 10:49       ` revcomninos
  2021-07-21 18:36         ` revcomninos
  0 siblings, 1 reply; 15+ messages in thread
From: revcomninos @ 2021-07-21 10:49 UTC (permalink / raw)
  To: 9fans

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

Awesome. Thank you. I can hardly believe you have managed to reduce all those commands to just a few. I will test this and revert back.
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M093404c48ec88dcc971cf6aa
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21 10:49       ` revcomninos
@ 2021-07-21 18:36         ` revcomninos
  2021-07-21 18:47           ` revcomninos
  0 siblings, 1 reply; 15+ messages in thread
From: revcomninos @ 2021-07-21 18:36 UTC (permalink / raw)
  To: 9fans

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

 I have tested all these solutions and they work. I can now apply the script (now significantly improved) from within sam and also from the command line with ssam. I will keep these posts as examples. The only question that remains is how to add comments to this script. If I use # it is ignored. 
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-Mcd0f0d6b71a7467bcf04d8ba
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21 18:36         ` revcomninos
@ 2021-07-21 18:47           ` revcomninos
  2021-07-21 20:40             ` Silas McCroskey
  0 siblings, 1 reply; 15+ messages in thread
From: revcomninos @ 2021-07-21 18:47 UTC (permalink / raw)
  To: 9fans

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

*not ignored
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-Mb735bb03a18063dea7599571
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21 18:47           ` revcomninos
@ 2021-07-21 20:40             ` Silas McCroskey
  2021-07-21 20:41               ` Silas McCroskey
  0 siblings, 1 reply; 15+ messages in thread
From: Silas McCroskey @ 2021-07-21 20:40 UTC (permalink / raw)
  To: 9fans

# is, for better or for worse, an address in sam:

          #n   The empty string after character n; #0 is the beginning
               of the file.

If you're talking about running the script with ^, it's just an
executable. You can put whatever you want in the shebang line; I tend
to use the following:

#!/bin/sed '/^#/d'
# this line is ignored
,x/\*|^ +| +$/d
# this line is also ignored
,x/  +//c/
,x/[‘‘’’“”‘’]/c/"
,x/\\p|\\b/c/\n\n\n
,x/\n\n+/c/\n

Running this as a script outputs only the lines in the file not
beginning with # for interpretation by sam.

To use it with ssam -f, you'd have to use the output rather than the
file directly.

In rc: ssam -f <{script}
In bash: ssam -f <(script)

both send the output to a temporary fd and then return a virtual file
path to that fd for commands like this that expect a file path.

If you really want comments within the sam language itself, the best I
came up with after 5 minutes of thinking was:

x/$^/!#

i.e. run a commented out shell command when an impossible pattern is
matched. The x// is needed to avoid actually launching the shell, and
the !# is needed to avoid interpreting the characters following as
another sam command (even if it's one that would never be run).

- Silas

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-Mafc9e9d330eaec6c6ad1fe81
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21 20:40             ` Silas McCroskey
@ 2021-07-21 20:41               ` Silas McCroskey
  2021-07-22  9:51                 ` revcomninos
  0 siblings, 1 reply; 15+ messages in thread
From: Silas McCroskey @ 2021-07-21 20:41 UTC (permalink / raw)
  To: 9fans

> #!/bin/sed '/^#/d'

apologies. quotes do not belong there.

#!/bin/sed /^#/d

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-Mfff65af821d77519fd6c8034
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21 20:41               ` Silas McCroskey
@ 2021-07-22  9:51                 ` revcomninos
  0 siblings, 0 replies; 15+ messages in thread
From: revcomninos @ 2021-07-22  9:51 UTC (permalink / raw)
  To: 9fans

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

Thank you so much Silas for your detailed and informative response. I have settled on accepting your second solution x/$^/!# for the time being. It gives me just the result I need. I use the commented lines mostly as a mnemonic device to recall the meaning of complex commands. 
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M95f7688f18e84b201bee87fb
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

* Re: [9fans] Help with a sam cleanup script
  2021-07-21 10:09   ` umbraticus
@ 2021-07-26 14:17     ` revcomninos
  0 siblings, 0 replies; 15+ messages in thread
From: revcomninos @ 2021-07-26 14:17 UTC (permalink / raw)
  To: 9fans

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

On Wednesday, 21 July 2021, at 12:09 PM, umbraticus wrote:
> also worth mentioning the ^ command if you're using 9front's sam.
put your sam commands in script like this:

#!/bin/rc
cat << STOP
x/  +/c/ 
x/\*/d
STOP
I cannot get ^script to work from within sam. I do have 9front's sam installed but there does not seem to be a ^ command in the version I am using. I am running sam as a standalone on Debian Linux. Can you provide a concrete example?
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T10b1d559ae7d981e-M6a9e47f734477d40f02037db
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

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

end of thread, other threads:[~2021-07-26 14:17 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-21  9:17 [9fans] Help with a sam cleanup script revcomninos
2021-07-21  9:24 ` Rodrigo G. López
2021-07-21  9:24 ` Rob Pike
2021-07-21  9:28 ` umbraticus
2021-07-21 10:02   ` revcomninos
2021-07-21 10:19     ` umbraticus
2021-07-21 10:49       ` revcomninos
2021-07-21 18:36         ` revcomninos
2021-07-21 18:47           ` revcomninos
2021-07-21 20:40             ` Silas McCroskey
2021-07-21 20:41               ` Silas McCroskey
2021-07-22  9:51                 ` revcomninos
2021-07-21  9:52 ` Alexander Kapshuk
2021-07-21 10:09   ` umbraticus
2021-07-26 14:17     ` revcomninos

9fans - fans of the OS Plan 9 from Bell Labs

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/9fans

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 9fans 9fans/ http://inbox.vuxu.org/9fans \
		9fans@9fans.net
	public-inbox-index 9fans

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.9fans


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git