5

I need to put one text inside another text.

1) I have a file with list of input values:

A1
B2
C3
D4
E5
  1. I have a wrapper pattern which should contain the text:
$wgSpecialPageLockdown['INPUT_COMES_HERE'] = array('sysop');

For each input value, a wrapper with input should be created, so the final result should be a file with:

$wgSpecialPageLockdown['A1'] = array('sysop');
$wgSpecialPageLockdown['B2'] = array('sysop');
$wgSpecialPageLockdown['C3'] = array('sysop');
$wgSpecialPageLockdown['D4'] = array('sysop');
$wgSpecialPageLockdown['E5'] = array('sysop');

I am open to do that in GUI as well, such as Visual Studio Code.

How would you prefer to do such an action?
And, by the way, how is such a textual operation commonly named?

9 Answers 9

10

If you just want to wrap each line with $wgSpecialPageLockdown[' before and '] = array('sysop'); after, try:

sed "s/.*/\$wgSpecialPageLockdown\['&'\] = array('sysop');/" filename.txt

I'd call that "wrapping", but I'm not sure there's a standard name for it.

2
  • Yes, that is what I want. In filename.txt there should be a simple text list with all inputs, right?
    – Lahor
    Mar 11, 2022 at 0:27
  • @Lahor Yep that's right.
    – frabjous
    Mar 11, 2022 at 0:51
6

One way can be :

## define the format to give to printf
fmt="\$wgSpecialPageLockdown['%s'] = array('sysop');\\n"

< infile xargs printf "$fmt" > outfile
5

You can use awk:

$ awk '{ print "$wgSpecialPageLockdown[\47"$0"\47] = array(\47sysop\47);" }' file > newfile
$wgSpecialPageLockdown['A1'] = array('sysop');
$wgSpecialPageLockdown['B2'] = array('sysop');
$wgSpecialPageLockdown['C3'] = array('sysop');
$wgSpecialPageLockdown['D4'] = array('sysop');
$wgSpecialPageLockdown['E5'] = array('sysop');
6
  • In file there should be a simple text list with all inputs, right?
    – Lahor
    Mar 11, 2022 at 0:26
  • @Lahor yes. Alternatively, the list could be piped to awk from stdin in which case omit file. Mar 11, 2022 at 0:40
  • @Lahor I just noticed you wanted the wrapped results written to a file. I've updated the code to have an output file. Mar 11, 2022 at 1:19
  • 1
    \47 is the easiest way to get awk to print a ' character (\47 is octal code for '). Mar 11, 2022 at 4:22
  • 1
    @Lahor see awk.freeshell.org/PrintASingleQuote
    – Ed Morton
    Mar 11, 2022 at 15:51
3

Given you are using VS Code and are open to a GUI option, figured I would post such an option in case it is useful.

You can accomplish this using the mutli-line editing feature which VS Code supports:

Copy and Paste using Multi-line Editing

(note: for me it is activated with Alt+click/drag. It is possible yours might be set up for Ctrl+click/drag, this is configured under Selection->Switch to Ctrl/Alt click for multi-cursor).

Essentially you can copy the first part of your string, Alt+drag down to select multiple lines of data, then paste at the start. Repeat with the end of the string. This can be done also on data of different lengths by pressing End to move all cursors to the end of the line

2

#!/usr/bin/python
k=open('file1','r')
for i in k:
    print "$wgSpecialPageLockdown['"+i.strip()+"'] = array('sysop');"

output


$wgSpecialPageLockdown['A1'] = array('sysop');
$wgSpecialPageLockdown['B2'] = array('sysop');
$wgSpecialPageLockdown['C3'] = array('sysop');
$wgSpecialPageLockdown['D4'] = array('sysop');
$wgSpecialPageLockdown['E5'] = array('sysop');
1

In PHP they are just called arrays but also typically go by names like "hash table", "hash map", "table", "map", "associative arrays", etc in other languages with maps and keys.

So you could have:

$key = 'DoubleRedirects';
$wgSpecialPageLockdown[$key] = array('sysop');

Of course you can get $key from any source like reading from a text file or other parameters.

1

Do that inside vi or vim:

:%s/.*/$wgSpecialLockdown['&']+ = arrray('sysop');/

if you're in insert mode exit by hitting ESC then hit : and paste the above (without the leading :).

You can even apply the sobstitutions to a selected range of line.

Eg: to apply on the first two lines only :

1,2s/.*/$wgSpecialLockdown['&']+ = arrray('sysop');/ 
0

Frame Challenge

This is PHP code you are trying to generate. Don't. Just write a loop. A simple form might be:

foreach (array('A1','B2','C3','D4','E5') as $k)
    $wgSpecialPageLockdown[$k] = array('sysop');

An easier to use form might be:

foreach (explode(' ','A1 B2 C3 D4 E5') as $k)
    $wgSpecialPageLockdown[$k] = array('sysop');

If you need multiple lines of names, you could use something like:

foreach (preg_split('/\\s+/',
        'A1 B2 C3 
        D4 E5 F6
        G7 H8 I9
        J10') as $k) 
    $wgSpecialPageLockdown[$k] = array('sysop');
0

Using Raku (formerly known as Perl_6)

raku -pe 's{(.*)}=qq{\$wgSpecialPageLockdown[\x27$0\x27] = array(\x27sysop\x27);};' 

OR

raku -ne 'put .map: qq{\$wgSpecialPageLockdown[\x27} ~ * ~ qq{\x27] = array(\x27sysop\x27);};'  

Sample Input:

A1
B2
C3
D4
E5

Sample Output:

$wgSpecialPageLockdown['A1'] = array('sysop');
$wgSpecialPageLockdown['B2'] = array('sysop');
$wgSpecialPageLockdown['C3'] = array('sysop');
$wgSpecialPageLockdown['D4'] = array('sysop');
$wgSpecialPageLockdown['E5'] = array('sysop');

Above are answers coded in Raku, a member of the Perl-family of programming languages. Each answer uses a command line flag(s): the first answer uses the -pe autoprinting linewise flag(s), while the second answer uses the -ne non autoprinting linewise flag(s), with a leading put command to print output. Both answers take advantage of the fact that Raku allows user-defined delimiters: both answers use {…} curly brace delimiters.

In the first answer above, the familiar s/// idiom is used, with a new form provided in Raku: s{…}={…} (or s[…]=[…], or s「…」=「…」, etc.). This new form allows the programmer to determine quotation/interpolation via Raku's "Q-Language" (i.e. quotation construct notation). The qq method instructs Raku to interpolate variables and backslashed characters. Each line is captured in the left half of the s{…}=qq{…} operator using (.*) and stored in the $0 variable. Output is then wrapped (prepended/appended) with additional text in the right half of the s{…}=qq{…} operator, and autoprinted.

  • Raku uses 4 primary sigils: $, @, %, and &. Inside the qq operator these literal characters need to be backslash escaped, otherwise Raku will try to interpolate them. The exception is the @ array sigil: as a literal character @ doesn't need to be escaped (convenient for email addresses), but when printing an actual @array1 variable, you add empty indexing square brackets at the end to force interpolation, like so: @array1[].

The second answer above is even simpler. Basically each line is read, .map-ped into, and wrapped with text before ("prepend") and after ("append"). In Raku, string concatenation is accomplished with ~ tilde, and mapped input is represented by a * "whatever star". The * "whatever star" is used in lieu of using $_ topic variable inside a {…} curly_brace-denoted code block.

As noted in other answers, \x27 the hex representation of a single-quote character can be used in Raku, either as above, or as \x[27]. However if you tire of remembering hex codes, just spell out the character you want to insert like so: \c[APOSTROPHE] (or \c[QUOTATION], or whatever your difficult-to-insert character happens to be). Here's what those answers look like:

raku -pe 's{(.*)}=qq{\$wgSpecialPageLockdown[\c[APOSTROPHE]$0\c[APOSTROPHE]] = array(\c[APOSTROPHE]sysop\c[APOSTROPHE]);};' 

OR

raku -ne 'put .map: qq{\$wgSpecialPageLockdown[\c[APOSTROPHE]} ~ * ~ qq{\c[APOSTROPHE]] = array(\c[APOSTROPHE]sysop\c[APOSTROPHE]);};' 

Addendum: Did I mention doublequotes were sytactic sugar for qq quoting? So, while the above answers work nicely for our Windows friends, the doublequotes below also work on appropriate (Linux, Mac) systems:

raku -pe 's[(.*)] = "\$wgSpecialPageLockdown[\x27$0\x27] = array(\x27sysop\x27);";' 

OR

raku -ne 'put .map: "\$wgSpecialPageLockdown[\x27" ~ * ~ "\x27] = array(\x27sysop\x27);";' 

https://docs.raku.org/language/quoting
https://raku.org

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .