Inserting lines between specific other lines in a file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Inserting lines between specific other lines in a file
# 1  
Inserting lines between specific other lines in a file

Hi,


I need to edit ssl.conf and add a few customized lines below the variable "Listen" at the top of the file.

I tried a few ways to do it, however failed to accomplish what I need.



These are the lines that I need to add into the file after the top most "Listen" variable in the file.


Code:
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-XSS-Protection "1; mode=block"
#Header always set Referrer-Policy "same-origin"
Header always set X-Permitted-Cross-Domain-Policies "master-only"
Header unset X-Powered-By
#Header set X-Content-Type-Options "nosniff"

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript application/x-javascript text/css application/javascript

# enable expirations
     ExpiresActive On

# expire files after a day in the client's cache
# expire files after 10 days in the client's cache
     ExpiresByType image/gif A864000
     ExpiresByType image/jpeg A864000
     ExpiresByType image/jpg A864000
     ExpiresByType image/png A864000
     ExpiresByType text/javascript A864000
     ExpiresByType application/x-javascript A864000
     ExpiresByType text/css A864000
     ExpiresByType image/x-icon A864000
     ExpiresByType application/javascript A864000

I use the steps below to add the lines above :


1) arg=$(grep -o -m 1 Listen ssl.conf | head -1)
2) vi add.txt


Code:
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-XSS-Protection "1; mode=block"
#Header always set Referrer-Policy "same-origin"
Header always set X-Permitted-Cross-Domain-Policies "master-only"
Header unset X-Powered-By
#Header set X-Content-Type-Options "nosniff"

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript application/x-javascript text/css application/javascript

# enable expirations
     ExpiresActive On

# expire files after a day in the client's cache
# expire files after 10 days in the client's cache
     ExpiresByType image/gif A864000
     ExpiresByType image/jpeg A864000
     ExpiresByType image/jpg A864000
     ExpiresByType image/png A864000
     ExpiresByType text/javascript A864000
     ExpiresByType application/x-javascript A864000
     ExpiresByType text/css A864000
     ExpiresByType image/x-icon A864000
     ExpiresByType application/javascript A864000

3) sed -i '/$arg/r add.txt' ssl.conf


The steps above do not work.


I then tried this :


Code:
zabbix-cj:/opt/rh/httpd24/root/etc/httpd/conf.d# awk '/$arg/{print $0 RS \
> "Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"" RS \
> "Header always set X-XSS-Protection "1; mode=block"" RS \
> "Header always set X-Permitted-Cross-Domain-Policies "master-only"" RS \
> "Header unset X-Powered-By";next}1' ssl.conf

This throws error :


Code:
awk: cmd. line:2: "Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"" RS \
awk: cmd. line:2:                                                      ^ syntax error

Please help correct the syntax for the steps I took above (if possible), else suggest another way that will produce what I need.

Last edited by Scrutinizer; 4 Weeks Ago at 05:32 PM.. Reason: Additional code tags
# 2  
You had the wrong quotes.
Code:
sed -i "/$arg/r add.txt" ssl.conf

and the shell will substitute the $arg.

--- Post updated at 11:14 ---

But if you have multiple Listen and want to insert test after the first one only, then it is better to get a line number:
Code:
# Get the line numbers for "Listen" and select the 1st one
lineno=$(sed -n '/Listen/=' ssl.conf | sed -n '1p')
# After that line number insert the text
sed -i "${lineno}r add.txt" ssl.conf

# 3  
How about this one, no $arg needed:
Code:
sed -e '1,// {/Listen/r add.txt' -e'}' ssl.conf

which checks from line #1 till /Listen/ is encountered the first time. The trick is "reusing" the "previous regular expression" for the range end definition. /Listen/ will be evaluated for line 1, and can be reused from then on.
# 4  
Quote:
Originally Posted by MadeInGermany
You had the wrong quotes.
Code:
sed -i "/$arg/r add.txt" ssl.conf

and the shell will substitute the $arg.

--- Post updated at 11:14 ---

But if you have multiple Listen and want to insert test after the first one only, then it is better to get a line number:
Code:
# Get the line numbers for "Listen" and select the 1st one
lineno=$(sed -n '/Listen/=' ssl.conf | sed -n '1p')
# After that line number insert the text
sed -i "${lineno}r add.txt" ssl.conf


This solution worked for me. However, I forgot about adding a new line (empty line) after the "Listen" string.
So the solution above works, but it includes the lines from add.txt immediately (the following line) after the Listen string.


I tried this :


Code:
sed -i "${lineno} a \n" ssl.conf

It creates the empty line, but also has \n in that new line.
How do I create only the empty line, without the \n?
# 5  
Most easy:
let the add.txt begin with an empty line!


If you really want an extra command to add the empty line, then...


The \n handling is a bit strange in GNU sed. The following seems to work:

Code:
sed -i "${lineno} a \\\\" ssl.conf

Within a "string" a literal \ needs \\ for the shell to work. Or use a concatenation of "string1" and 'string2'; the sed sees the concatenated string:

Code:
sed -i "${lineno}"' a \\' ssl.conf

It is more efficient to do two actions in one stroke. The folllowing is a multi-liner that uses the "correct" (Unix-sed style) appending of an empty line:
Code:
sed -i "$lineno"' {
a \

r add.txt
}' ssl.conf

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #719
Difficulty: Medium
Alan Minsky wrote the book Artificial Neural Networks, attacking the work of Frank Rosenblatt, which became the foundational work in the analysis of artificial intelligence applications in machine learning.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Inserting lines from one file to a sorted list

Hello friends! I am working a Psychology/Neuro* project where I am sorting inline citations by category. The final step of the process has me a little stuck. I need to take citations from a text list and sort them in another text file. Here is a file X example... (1 Reply)
Discussion started by: danbroz
1 Replies

5. UNIX for Advanced & Expert Users

Inserting duplicate lines in a file

Hi, I copied the contents of a binary file into a .text file using hd (hexdump) command. The data in binary file is such that I get in many places like following 00000250 00 00 00 00 3f 2d 91 68 3f 69 fb e7 00 00 00 00 |....?-.h?i......| 00000260 00 00 00 00 00 00 00 00 00 00 00 00 00... (2 Replies)
Discussion started by: KidD312
2 Replies

6. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

7. Shell Programming and Scripting

Substitute specific lines with lines from another file

Hello All, I am new to this forum. I am currently facing a problem in manipulating files. I have two files called old-matter and new-matter # cat old-matter abc: this, is a, sample, entry byi: white board, is white in color rtz: black, board is black qty: i tried, a lot asd: no... (1 Reply)
Discussion started by: rahmathulla
1 Replies

8. Shell Programming and Scripting

Inserting lines in between the contents of file.

Hi, I want to insert some lines in between the contents of a file but the file format should not be changed. #!/usr/bin/sh - # Link appropriate OS specific versions of vxicap and vxchk4badblks vxlvmlink() { vxipath=/usr/lib/vxvm/bin relmajor=`uname -v` ... (1 Reply)
Discussion started by: ajilesh
1 Replies

9. Shell Programming and Scripting

inserting a lines in a file

Hi folks, i need to insert the same set of lines between each line input lines 111111 aaaaaa 333333 output should be 111111 1 2 3 aaaaaa 1 2 3 333333 1 (2 Replies)
Discussion started by: Balaji Sukumara
2 Replies

10. UNIX for Dummies Questions & Answers

Inserting specific lines in vi

How can I insert, say lines 500 - 700 from another file into the current file on the current line (cursor) that I am editing while in vi (AIX). I know how to insert the entire file but how do you do it when you only need certain lines from a huge file? I've referenced my Unix Unleash book but... (2 Replies)
Discussion started by: giannicello
2 Replies

Featured Tech Videos