How to remove a place holder in a text file with text from another file when multiple lines are present?

Somenath Sinha

I have a file:

  - id: 1.4.25.2
    name: 'Configuring a VNC Server'
    permalink: '/rhcsa/managing-network-services/configuring-vnc-access/configuring-a-vnc-server'
    description: '<*description>'
    content: []
  - id: 1.4.25.3
    name: 'Connecting to a VNC Server'
    permalink: '/rhcsa/managing-network-services/configuring-vnc-access/connecting-to-a-vnc-server'
    description: '<*description>'
    content: []

I have to replace each <*description> with some text. Obviously, I thought of using regex. Apparently (according to certain answers on this site), sed doesn't have a non-greedy modifier for substitutions. Thus, I tried using perl:

(.*id: 1\.4\.25\.2(?:\n|.)*)\'(\<\*description\>)\'

doesn't select the required part, which would be from - id: 1.4.25.2 till description: '<*description>'\ncontent: [], right before the next element in the yaml array, i.e., before the line - id: 1.4.25.3. I can't figure out a way to do this, and how I could change the description of each entry in the file with a custom text lifted from some place else!

thrig

Use a YAML module; recurse through the data structure and replace any matching elements here with lines read in on standard input.

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use YAML::Tiny;

my $yaml =
  YAML::Tiny->read( $ARGV[0] // die "Usage: $0 yaml-file [out-file]\n" );

mangle_description( $yaml->[0] );

$yaml->write( $ARGV[1] // "$ARGV[0].out" );

sub mangle_description {
    my $what = shift;
    my $type = ref $what;
    if ( $type eq 'HASH' ) {
        for my $key ( keys %$what ) {
            if ( $key eq 'description'
             and $what->{$key} eq '<*description>' ) {
                $what->{$key} = set_description();
            }
            mangle_description( $what->{$key} ) if ref $what->{$key};
        }
    } elsif ( $type eq 'ARRAY' ) {
        for my $entry (@$what) {
            mangle_description($entry);
        }
    } else {
        warn Dumper $what;
        die "unknown type in YAML ??\n";
    }
}

sub set_description {
    my $next = readline *STDIN;
    chomp $next;
    return $next;
}

the above saved as parser and with valid YAML in input:

$ yes | perl parser input
$ grep description input.out
    description: y
    description: y
$ 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I remove all the lines in a text file that are present in another text file in Windows?

From Dev

Scala: How to remove blank lines when reading text from file

From Dev

How to remove specific lines from a text file?

From Dev

How to use Ruby's command line in-place-edit mode to remove lines from a text file

From Dev

Regex to filter and remove specific multiple lines of text from a file with python

From Dev

How do I remove matching lines from one text file using another?

From Dev

Delete multiple lines from file when text is found

From Dev

How to remove blank lines from text file using powershell

From Dev

How to remove particular words from lines of a text file?

From Dev

How to remove particular words from lines of a text file?

From Dev

How can I remove all lines not containing `@` from a text file?

From Dev

How to remove blank lines from text file using powershell

From Dev

How to remove duplicate lines from a large text file efficiently?

From Dev

Remove lines from text file if it starts with

From Dev

Remove odd or even lines from a text file

From Dev

Remove particular lines from a text file in R

From Dev

Remove lines from text file if it starts with

From Dev

Remove empty lines from a text file

From Dev

replace lines in a text file from another text file

From Dev

Delete lines from a text file that match multiple regexes on a text file

From Dev

How to remove duplicate lines inside a text file?

From Dev

Python - Parsing multiple lines of text from file

From Dev

Returning multiple lines from a text file in python

From Dev

How to find and replace multiple lines in text file?

From Dev

How to append multiple lines of text to a file?

From Dev

Remove duplicate lines in a text file

From Dev

Remove Duplicate Lines in Text File

From Dev

Excel: how to remove multiple file paths from text

From Dev

How to remove multiple blank lines from a file?

Related Related

  1. 1

    How do I remove all the lines in a text file that are present in another text file in Windows?

  2. 2

    Scala: How to remove blank lines when reading text from file

  3. 3

    How to remove specific lines from a text file?

  4. 4

    How to use Ruby's command line in-place-edit mode to remove lines from a text file

  5. 5

    Regex to filter and remove specific multiple lines of text from a file with python

  6. 6

    How do I remove matching lines from one text file using another?

  7. 7

    Delete multiple lines from file when text is found

  8. 8

    How to remove blank lines from text file using powershell

  9. 9

    How to remove particular words from lines of a text file?

  10. 10

    How to remove particular words from lines of a text file?

  11. 11

    How can I remove all lines not containing `@` from a text file?

  12. 12

    How to remove blank lines from text file using powershell

  13. 13

    How to remove duplicate lines from a large text file efficiently?

  14. 14

    Remove lines from text file if it starts with

  15. 15

    Remove odd or even lines from a text file

  16. 16

    Remove particular lines from a text file in R

  17. 17

    Remove lines from text file if it starts with

  18. 18

    Remove empty lines from a text file

  19. 19

    replace lines in a text file from another text file

  20. 20

    Delete lines from a text file that match multiple regexes on a text file

  21. 21

    How to remove duplicate lines inside a text file?

  22. 22

    Python - Parsing multiple lines of text from file

  23. 23

    Returning multiple lines from a text file in python

  24. 24

    How to find and replace multiple lines in text file?

  25. 25

    How to append multiple lines of text to a file?

  26. 26

    Remove duplicate lines in a text file

  27. 27

    Remove Duplicate Lines in Text File

  28. 28

    Excel: how to remove multiple file paths from text

  29. 29

    How to remove multiple blank lines from a file?

HotTag

Archive