2

I'm attempting to split a .php file into several parts using bash, and I can't seem to figure out a way to split it based on the header.

I will not be changing anything within the .php file, only taking the information from it.

I have looked into using split, but it doesn't seem to have the attributes that I am looking for.

As an example, the file behaves like this:

'Josh-per' => array(
     Height=60
     Weight=90
     Age=20
),
'Katie-per' => array(
     Height=55
     Weight=110
     Age=45

#...etc

This isn't the exact code, but is much more basic but retains the same idea. Each "name" ends with -per, and then holds information relating to that person inside it.

What I'd like it to do is turn each Array into its own file, named Josh.foo and Katie.foo

With the inside being

Height=60
Weight=90
Age=20

=========================

Ok, I was a bit confusing initially and will try to remedy that, but I'll keep the old one up anyway just in case.

So, I was given a .php with multiple classes, starting with $NAME-per, but it is not unique in the file. I am not going to be changing anything within the .php, as it is autogenerated with a lot more information than what I am presenting.

Here is a better representation of what is in the file, named sources.php

<?php
$config = array(

'Josh-per' => array(
     'certificate' => 'null.cert',
     'key' => 'something-per.key',
     'site' => 'website.com',
),
'Katie-per' => array(
     'certificate' => 'notnull.cert',
     'key' => 'testkey.key',
     'site' => 'fakesite.com'
#...etc

In summary, what I am trying to do is make Josh-per into a new file called Josh.sources, and Katie-per into Katie.sources

Kazken
  • 31
  • 1
  • 6
  • is `-per` a unique substring that will always be contained within the line(s) that should start new file(s) and not exist on any other lines? – jeremysprofile Apr 29 '19 at 17:45
  • In my specific circumstance, no. There are some instances where it will link to another person, like #... Age=20 Friend=Katie-per ...# – Kazken Apr 29 '19 at 17:48
  • @Martin I could make the .php into a .txt to make it easier, but it should be done using bash. – Kazken Apr 29 '19 at 17:54
  • @tripleee I don't think Kazken's answer qualifies as a duplicate. The one you linked to is specifically asking for a regex solution, this one does not require regex. By pointing to a regex-specific solution, you're more likely to lead people down the rabbit-hole that is regex when it's not needed. – lilHar Apr 29 '19 at 18:20
  • @triplee I edited the body to make more sense, I believe I was missing a few components in my initial question which makes it different from the one you linked – Kazken Apr 29 '19 at 18:21
  • @liljoshu Why do you think regex is wrong here and what would you suggest instead? – tripleee Apr 30 '19 at 04:11
  • @tripleee A manager of mine once had a saying, "I have a problem. I have solved it with regex. Now I have two problems." Specifically, using a regex solution, although good for immediate efficiency of code, is very bad for future maintainability and readability of code. As far as what I would suggest, I've put my own suggestion down below. Since he's moving between bash and php, both already have methods for splitting that don't rely on regex, so relying on regex is adding needless complexity. Marking it as duplicate and pointing to regex-only limits to potentially less-optimal solutions. – lilHar Apr 30 '19 at 16:46
  • 1
    Apparently the OP is not specifically interested in using PHP for this at all, and the regex will be rather trivial. For the origins of the saying, http://regex.info/blog/2006-09-15/247 – tripleee Apr 30 '19 at 17:09
  • @tripleee Yes, the author made that point clear later. That said, there is still no reason to shoehorn answers to this problem to only using regex when standard bash can handle it fine. The answer CAN use regex, that doesn't mean it SHOULD or HAS to, unlike the linked question. And closing as duplicate gives that false impression. That said, I do appreciate you coming back to discuss whether or not it should be marked duplicate. – lilHar Apr 30 '19 at 19:34

2 Answers2

0

You don't need to use bash to split, you can do so while it's still in PHP.

If what you're trying to split is a string:

Operating under the assumption what you're showing isn't array (as it isn't formatting like a proper array in php).

    // $string is where you're getting this particular formatted information.

    $pieces = explode ($newstring, ',');
    // $piece[0] and $piece[1] will now give you each value.

If that's an array...

If what youre splitting is actually a multi-dimensional array, it should be formatted like this:

$pieces = array('Josh-per' => array(
     Height => 60,
     Weight => 90,
     Age => 20
),
'Katie-per' => array(
     Height => 55,
     Weight => 110,
     Age=45 );

#...etc

At which point it's already split

// $pieces[0] and $pieces[1], etc.

If you have to split in Bash from that print-out...

If you HAVE to split using bash, PHP uses the comma to deliminate fields when printing an array. Normally, that should affect ALL fields, but the example you're showing has it only happening on the higher level of the multi-dimensional array (may indicate a bug later that needs fixed, but for the moment, it works in your favor by giving you a solid delimeter. For this, there's a solution suggested here:

How do I split a string on a delimiter in Bash?

# after you take your input into PERSON
 while IFS=',' read -ra PERSON; do
      for i in "${PERSON[@]}"; do
          # process "$i"
      done
 done <<< "$IN"
# do what you do with your array

and you just split on the comma. This will give your output the following formatting:

'Josh-per' => array(
     Height=60
     Weight=90
     Age=20
)

At which point you'll want to do the split-on-delimeter twice more. Once on the "(" and take just what comes after, and with the results again on ")" and take just what comes before. Then output and you've got your file.

lilHar
  • 1,735
  • 3
  • 21
  • 35
  • 1
    I *think* that it is in proper array format, (I normally don't use PHP) but I could turn the file into a .txt as I won't be editing anything inside of it. All the information is auto-generated, and im trying to make a script to split it out into separate files so I can run code on those. – Kazken Apr 29 '19 at 17:59
  • Ah, you might want to modify your question to state you're not altering the php then. – lilHar Apr 29 '19 at 18:00
  • 1
    I just updated it, thanks. Sorry it was a bit confusing – Kazken Apr 29 '19 at 18:07
  • You're new on stackoverflow, as long as you learn. Clarity in questions is super-important. :) – lilHar Apr 29 '19 at 18:09
  • Oh, and remember to upvote people who help you that aren't giving outright horrible answers, even if they aren't the one you choose (the one you choose as right gets even more points.) – lilHar Apr 29 '19 at 18:13
0

This one is using awk: Here filename is the name of input file. As you mentioned that this is not the actual input , so you may need to adjust the column numbers as per the actual input.

awk -v RS=")," '{$1=$1}1' filename |awk -v FS="[-' ]"   -v OFS="\n" 'length($2){print  $7, $8, $9 > $2".foo"}' 


 -->cat Josh.foo
Height=60
Weight=90
Age=20

 -->cat Katie.foo
Height=55
Weight=110
Age=45
P....
  • 17,421
  • 2
  • 32
  • 52