You are not logged in.

#1 2023-02-25 03:30:46

archisominimalist
Member
Registered: 2023-02-11
Posts: 22

paragraph editing

https://i0.wp.com/jeffreifman.com/wp-co … =460%2C242

This gif shows the level of productivity I'm aiming at, now what do you call the following, paragraph editing?, without words I can't use a search engine.

    {
        nodeId: '1-2-1',
        parentNodeId: '1-2',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },
    {
        nodeId: '1-2-2',
        parentNodeId: '1-2',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },

    {
        nodeId: '1-3',
        parentNodeId: 'O-1',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },
    {
        nodeId: '1-3-1',
        parentNodeId: '1-3',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },

I would love to edit hundreds of these paragraphs manually.
Each time nodeId, parentNodeId and 'content' have to be edited slightly.
One Paragraph is:

    {
        nodeId: '1-4-2',
        parentNodeId: '1-4',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },

I only know how to use my mice or arrow keys, how does utilize hotkeys and/or macros to be more productive.
Or should I rather create a script that, I don't know how to approach this.

Last edited by archisominimalist (2023-02-25 10:31:54)


Thanks to everybody that is contributing to make open-source software possible.
Apparently I still manage to offend people, even while trying to be as empathic as possible and search/try to learn before posting.
I love y'all.

Offline

#2 2023-02-25 03:43:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,522
Website

Re: paragraph editing

The gif you linked would be block editing in vim or a vim-like editor.  However that would not likely work for your usecase as the elements you want to edit are not in a block (there are intervening lines with braces).  But I strongly suspect you could do the job quite easily with sed.  Learning vim and sed may be overkill for your current goal, but doing so will pay off in the long run.

If you elaborate on how these need to be "edited slightly" I could likely give more specific pointers.

Last edited by Trilby (2023-02-25 03:43:46)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2023-02-25 04:09:43

archisominimalist
Member
Registered: 2023-02-11
Posts: 22

Re: paragraph editing

I'd love to learn sed.

For example

    {
        nodeId: '1-1',
        parentNodeId: '1-1',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    }, 

should become

    {
        nodeId: '1-1-1',
        parentNodeId: '1-1-2',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    }, 

Last edited by archisominimalist (2023-02-25 10:31:35)


Thanks to everybody that is contributing to make open-source software possible.
Apparently I still manage to offend people, even while trying to be as empathic as possible and search/try to learn before posting.
I love y'all.

Offline

#4 2023-02-25 04:37:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,522
Website

Re: paragraph editing

But what is the logic to that example?  Is there a rule / set of rules by which these changes are made.  You added a "-1" to the end of nodeId; should every nodeId get a "-1" added, or will it be "-2" or some other number for some of them?  And for parentNodeId you added "-2" ... same question here.  And from where does "else" come?

If you can define the logic of the changes that need to be made, it should be very easy to get a script (e.g., sed) to do the change instantaneously on nearly any number of "paragraphs".


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2023-02-25 10:30:36

archisominimalist
Member
Registered: 2023-02-11
Posts: 22

Re: paragraph editing

Sorry Trilby for messing up there, edited hope it's unified now.

I found this, promising code snippet, hoping that this clears up what's the purpose.
Trying to adjust it to work with the paragraphs in my case.

 
Current values for
block :
nodeId: 
parentNodeId: 
template: templateDefault('')
Block  updated successfully.

Current output appears instantly without chance for user to input.
I suspect:
A: that the 'RegEx' is not correct, therefore only the echo commands but that does not make any sense either.
B: block is never actually defined as variable, nor is new_nodeId, new_parentNodeId or new_template

 while read -r block; do
#!/bin/bash

# Specify the file path and the search pattern
file_path="/home/archie/project/assets/scripts/data.js"
search_pattern="{nodeId: '[^']*',\nparentNodeId: '[^']*',\ntemplate: templateDefault('[^']*')\n}"

# Search for blocks that match the pattern and loop through them
while read -r block; do
  # Extract the values inside single quotes using sed
  nodeId=$(echo "$block" | sed -n "s/.*nodeId: '\([^']*\)',.*/\1/p")
  parentNodeId=$(echo "$block" | sed -n "s/.*parentNodeId: '\([^']*\)',.*/\1/p")
  template=$(echo "$block" | sed -n "s/.*template: templateDefault('\([^']*\)').*/\1/p")

  # Print the current values of nodeId, parentNodeId, and template
  echo "Current values for"
  echo "block $nodeId:"
  echo "nodeId: $nodeId"
  echo "parentNodeId: $parentNodeId"
  echo "template: templateDefault('$template')"

  # Ask the user to input new values
  read -p "Enter new nodeId for block $nodeId: " new_nodeId
  read -p "Enter new parentNodeId for block $nodeId: " new_parentNodeId
  read -p "Enter new template for block $nodeId: " new_template

  # Replace the old values with the new values using sed
  sed -i "s/nodeId: '$nodeId',/nodeId: '$new_nodeId',/g" $file_path
  sed -i "s/parentNodeId: '$parentNodeId',/parentNodeId: '$new_parentNodeId',/g" $file_path
  sed -i "s/template: templateDefault('$template')/template: templateDefault('$new_template')/g" $file_path

  echo "Block $nodeId updated successfully."
done <<< "$(grep -oP "$search_pattern" $file_path)"
    {
        nodeId: '1-2-1',
        parentNodeId: '1-2',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },
    {
        nodeId: '1-2-2',
        parentNodeId: '1-2',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },

    {
        nodeId: '1-3',
        parentNodeId: 'O-1',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },
    {
        nodeId: '1-3-1',
        parentNodeId: '1-3',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault('Company name', 'Revenue: $100.000.000', '%100')
    },

Last edited by archisominimalist (2023-02-25 11:17:29)


Thanks to everybody that is contributing to make open-source software possible.
Apparently I still manage to offend people, even while trying to be as empathic as possible and search/try to learn before posting.
I love y'all.

Offline

#6 2023-02-25 13:44:55

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,522
Website

Re: paragraph editing

That's not really helpful.  That's a needlessly complex script for what it appears intended to do - so there's little point in debugging why it might fail.  More importantly, I don't see how it is at all related to your intended goal.  Even if that script were to work as intended, it would just cycle through each "paragraph" and prompt you to manually type in values for each and every one - surely editing hundreds of entries this way wouldn't be any easier than just editing hundreds of them in text editor (I imagine it'd be harder).

My question is where do the new values come from.  If you are prompted to enter new values, how do you decide what value to enter?  Code (or scripts) only implement an agorithm.  It is up to you to define what the algorithm is.

If you want to just add "-1" to every nodeId that doesn't already have three components and add "-2" to every parentNodeId that doesn't already have a three components, that'd just be this:

sed "/nodeId/s/'\([0-9]\+-[0-9]\+\)'/'\1-1'/;/parentNodeId/s/'\([0-9]\+-[0-9]\+\)'/'\1-2'/" yourfile.json

But I'm quite sure this is not what you want.  So until you define what you want, no one could help you code it.

EDIT: note that the underlined part is what I mean by the algorithm.  It can be in plain English, but it needs to define what is to be done with the data.  Plain English descriptions can be useful, though they can also result in programmers coming home from the groccery with six gallons of milk ...

Last edited by Trilby (2023-02-25 13:52:14)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2023-02-25 15:16:36

archisominimalist
Member
Registered: 2023-02-11
Posts: 22

Re: paragraph editing

Thank you for you excellent post.

Your explanation combined with the six gallons joke helped me understand and will shape how I will ask future questions, if I will still be allowed to ask.

I would love to edit hundreds of these paragraphs manually.

The new values need to be entered manually.
The reason why I'm making it more complicated now is to learn something new, for when I asked the question I did not know how to approach this, now I'd say the script is the cleanest way if the new values have to be user input.

The script seems like the cleanest way in the long run.
Probably there is a feature of an editor that can help to 'jump' the cursor from A --> B --> C / highlight what's written / user input.

     {
        nodeId: ' A ',
        parentNodeId: ' B ',
        nodeImage: { url: './assets/media/logo.jpg' },
        template: templateDefault(' C ')
    },

Thanks to everybody that is contributing to make open-source software possible.
Apparently I still manage to offend people, even while trying to be as empathic as possible and search/try to learn before posting.
I love y'all.

Offline

#8 2023-02-25 16:16:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,522
Website

Re: paragraph editing

archisominimalist wrote:

The new values need to be entered manually.

Why?  As you do it manually, how do you know what values to put in each block?  Where does the information actually come from?  If it is from some handwritten or hard-copy only source, then yes it'd need to be entered manually, but any other source (either another digital medium, or some logical manipulation of the existing data) then manual entry will be tedious and error prone.

archisominimalist wrote:

Probably there is a feature of an editor that can help to 'jump' the cursor from A --> B --> C / highlight what's written / user input.

Yes, there certainly is.  Any good editor will have this ability.  As a good editor, vi(m) and it's variants certainly do have such an ability.  Despite not being a good editor, I'm pretty sure emacs does too.  Learn one of these.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB