1

I have looked across StackOverflow and haven't found an exact answer to my problem. I am no sed master so I would appreciate your help.

I have 2 files:

header.txt which contains the bootstrap standard header html structure:

<nav class="navbar navbar-default">
    <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

and my Index.html:

<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="description" content="Free Web tutorials on HTML and CSS">
  <link href="https://fonts.googleapis.com/css?family=Work+Sans:100,200" rel="stylesheet">
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="css/jquery-ui.css">
  <link rel="stylesheet" href="css/main.css">
  <title>Document</title>


</head>
<body>






  <script src="js/jquery.js"></script>
  <script src="js/jquery-ui.js"></script>
  <script src="js/bootstrap.min.js" type="text/javascript"></script>
  <script src="js/main.js"></script>
</body>
</html>

my script: addheader

#!/bin/bash
catOut=$(cat header.txt)
sed "/<body>/ a $catOut" index.html

I am trying to append the structure to the body. (I know I need the -i option to actually append)

I keep getting an error with the $catOut because of the < tags in the header.txt file. It's trying to evaluate it or something.

sed: -e expression #1, char 19: unknown command: `<'
Matthew
  • 129
  • 1
  • 3
  • 9
  • Wouldn't [Insert contents of a file after specific pattern match"](https://stackoverflow.com/questions/16715373/) be the solution? – U880D Jan 02 '18 at 20:05
  • It doesn't need to be sed, but it needs to append the contents to a certain part of the file. I think I can do it with a for loop but still need to figure it out. – Matthew Jan 02 '18 at 20:07
  • Note that this is generally buggy if your text file isn't pre-escaped and you want to guarantee that the result is valid HTML -- things like escaping `&` to `&` require HTML-aware tools. (If the body file *is* already HTML, using a `.txt` extension is arguably a bit misleading). – Charles Duffy Jan 02 '18 at 20:18
  • Thanks for the help Charles. That's great info, I will keep that in mind as I get better with using these techniques. This is mainly just for practice right now. – Matthew Jan 03 '18 at 01:59

1 Answers1

1
sed '/<body>/r header.txt' index.html

r command reads the content of header.txt and inserts after <body> has been found. More about the r command in GNU sed manual or this excellent grymoire.com tutorial.


Solution using only bash:

reg='<body>' 
while IFS= read -r line; do
    printf '%s\n' "$line"
    [[ $line =~ $reg ]] && cat header.txt
done < index.html > new_index.html
PesaThe
  • 7,259
  • 1
  • 19
  • 43