-5

The rules would be:

  • Delete all lines except the last line which contains: link and href=
  • Replace the contents of whatever is after: href= and before: .css with: hello-world
  • Must maintain no quotes, single quotes or double quotes around the file name

A few examples:

This is a source file with quotes:

<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/master.css">

This is the new source file:

<link rel="stylesheet" href="hello-world.css">

This is a source file without quotes:

<link rel=stylesheet href=css/reset.css>
<link rel=stylesheet href=css/master.css>

This is the new source file:

<link rel=stylesheet href=hello-world.css>

It does not need to maintain the path of the file name. It however cannot use <> brackets or spaces to determine what needs to be edited because the template language which is writing that line might not use brackets or spaces. The only thing that would remain consistent is href=[filename].css.

My bash/sed/regex skills are awful but those tools seem like they will probably get the job done in a decent way? How would I go about doing this?

EDIT

To clarify, the end result would leave everything above and below the lines that contain link and href= alone. Imagine that the source file was an html file or any other template file like so:

<html>
  <head>
    <title>Hello</title>
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/master.css">
  </head>

  <body><p>...</p></body>
</html>

It would be changed to:

<html>
  <head>
    <title>Hello</title>
    <link rel="stylesheet" href="css/hello-world.css">
  </head>

  <body><p>...</p></body>
</html>

The path of the CSS files might be anything too.

../foo/bar.css
http://www.hello.com/static/css/hi.css
/yep.css
ok.css

The new file's path would be supplied as an argument of the bash script so the regex should remove the path.

AntelopeSalad
  • 1,736
  • 1
  • 16
  • 27
  • 6
    [Does the *text* in that file happen to be HTML?](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Lix May 06 '12 at 16:21
  • This isn't a "here are my requirements, now do my job for me" site. State questions as precise problems/issues. – Bohemian May 06 '12 at 16:22
  • Really, 3 down votes but there's 35,693 regex questions asked which ask for solutions based on rules. Why are those not getting down voted? – AntelopeSalad May 06 '12 at 16:22
  • To be honest with you @ant - those question that are downvoted are usually becuase they do not meet the sites standards. The reason you do not see them is because they would have been closed and later deleted. – Lix May 06 '12 at 16:23
  • The main reason IMO for the downvotes is becuase you have not shown any attempt to solve this on your own before posting it here - http://meta.stackexchange.com/a/128553/172936. Oh - and for clarity I am not one of the voters... yet. – Lix May 06 '12 at 16:25
  • I asked because I don't know the answer to the problem. If I knew how to solve it precisely then I wouldn't have posted anything to begin with. – AntelopeSalad May 06 '12 at 16:25
  • **You are 100% correct regarding the tools that will be helpful to you.** Add `awk` and `grep` to the mix too :) Try doing some research on them first - you might find that it really is not that difficult. – Lix May 06 '12 at 16:26
  • I researched multiple sites for over an hour until I realized the best use of my time is to probably post a question because I wasn't making reasonable progress. – AntelopeSalad May 06 '12 at 16:28
  • What ever progress you made would have shown your research effort. Simply naming the correct tools does not show that. – Lix May 06 '12 at 16:29
  • Btw I spend 5 seconds looking at related posts and saw this: http://stackoverflow.com/questions/3740102/combine-two-particular-lines-using-sed 6 answers and no down votes but he didn't show any progress. He wrote out his expected results in plain text and people helped him. How is my post different? – AntelopeSalad May 06 '12 at 16:30
  • I've spend an additional 3 minutes looking into related posts a bit more and found many examples where not a single line of code was written but people got dozens of answers and upvotes for asking a specific question. Thoughts on that? – AntelopeSalad May 06 '12 at 16:35
  • My thoughts will not fit into a comment conversation here :) let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10934/discussion-between-lix-and-antelopesalad) – Lix May 06 '12 at 16:43
  • 2
    @AntelopeSalad - I doubt this kind of research will make people like your post better. Instead, you could by now have updated the post with what you've tried or how you would see it might work. That creates a better starting point and a better impression, so that people are more likely to invest their time to help. – Joanna Derks May 06 '12 at 17:09

3 Answers3

3

Following a discussion in chat, one solution using PHP as a command line script would look like this -

#! /usr/bin/php 
<?php

    $options = getopt("f:r:");
    $inputFile = $options['f'];
    $replacement = $options['r'];
    // read entire contents of input file 
    $inputFileContents = file_get_contents($inputFile);
    // setup the regex and execute the search
    $pattern = '/.*link.*href=["|\']?(.*[\\\|\/]?.*)\.css["|\']?.*/';
    preg_match_all($pattern, $inputFileContents, $matches);
    // remove last occurance of regex 
    // these are the lines we'll want to hang onto
    $matchedLines = $matches[0];
    array_pop($matchedLines);
    // isolate the last css file name
    $matchedFileName = array_pop($matches[1]);
    // first substitution replaces all lines with <link> with 
    // an empty string (deletes them)
    $inputFileContents = str_replace($matchedLines,'',$inputFileContents);
    // second substitution replaces the matched file name
    // with the desired string
    $inputFileContents = str_replace($matchedFileName,$replacement,$inputFileContents);
    //*/
      // save to new file for debugging
      $outputFileName = "output.html";
      $outputFile = fopen($outputFileName,'w+');
      fwrite($outputFile,$inputFileContents);
      fclose($outputFile);
    /*/
      // save changes to original file
      $origFile = fopen($inputFile,'w+');
      fwrite($origFile,$inputFileContents);
      fclose($origFile);
    //*/
    exit();
?>

You would execute this script from the command line like so -

$ php thisScript.php -f "input.html" -r "hello-world" 
  • -f is the input file that we are parsing.
  • -r is the replacement string for the css file name (in this example "hello-world").
Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131
  • 3
    If you don't want to read through all of it, Lix went out of his way to explain quite a bit to me and his solution results in the exact functionality of what the script needed to do. Now the fun part, porting it from PHP to perhaps a grep/sed or grep/sed/awk version. – AntelopeSalad May 06 '12 at 20:00
  • 1
    Lix ! where is my `fclose` ?! :-) missing `fclose($outputFile);` after `fwrite...` line. – Roni May 08 '12 at 15:44
1

Answer specifically, for this case:

If you include same css file twice, it does not create any harm as far as seen by the user.
So you may just replace both css/reset.css AND css/master.css by css/hello-world.css.

There may be better ways, but I found this a quick way. It will work specifically for this case & not if you want to replace <script> or other tags.

Helio
  • 1,035
  • 8
  • 16
anishsane
  • 20,270
  • 5
  • 40
  • 73
  • @user3374657: Why does it not apply for your answer? :D Never mind. But I do agree that my answer is only a hint & not complete solution. – anishsane May 19 '14 at 10:47
  • +1: Hint goes in a comment, not in an answer, but you have reason – Helio May 19 '14 at 11:18
0

Try including the first part of the file before the css and then include the rest of the file below the css, and in the middle, echo the correct css lines

Helio
  • 1,035
  • 8
  • 16