How to insert new line after a specific character in scripts?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to insert new line after a specific character in scripts?
# 1  
Old 02-02-2019
How to insert new line after a specific character in scripts?

Hi,


I'm trying to add a new line after finding a specific String.

That is my string:
volumes:
-
Code:
${DIR_WORK}/loadbalancer/html:/var/www/html


and I want to change that file to:
volumes:
-
Code:
${DIR_WORK}/loadbalancer/html:/var/www/html
extra_hosts:
      - "hostname:192.168.1.4"


That my code:



Code:
 lastLine="{DIR_WORK}/loadbalancer/html:/var/www/html"
 extraHost="extra_hosts:"
 sed 's~'"${lastLine}"'~a\'"${extraHost}"'~' $file


but that do nothing



do you have an idea!?
# 2  
Old 02-02-2019
Hi, try so
Code:
lastline='{DIR_WORK}\/loadbalancer\/html:\/var\/www\/html'
sed -i '/'"$lastline"'/ a \extra_hosts:' $file

# 3  
Old 02-02-2019
Quote:
Originally Posted by siamak
Hi,


I'm trying to add a new line after finding a specific String.

That is my string:
volumes:
-
Code:
${DIR_WORK}/loadbalancer/html:/var/www/html


and I want to change that file to:
volumes:
-
Code:
${DIR_WORK}/loadbalancer/html:/var/www/html
extra_hosts:
      - "hostname:192.168.1.4"


That my code:



Code:
 lastLine="{DIR_WORK}/loadbalancer/html:/var/www/html"
 extraHost="extra_hosts:"
 sed 's~'"${lastLine}"'~a\'"${extraHost}"'~' $file


but that do nothing



do you have an idea!?
Please always tell us what operating system and shell you're using when you start a new thread in the UNIX for Beginners Questions & Answers forum. Although many utilities are available on all BSD, Linux, and UNIX systems, the options they provide and the output they produce can vary from system to system and shell to shell. If we know what environment you're working in, it helps us make suggestions that will work in your environment without having to guess what your environment might be.

It isn't at all clear to me what you're trying to do.

Does the file you want to edit actually contain the text ${DIR_WORK} or does the file contain what the expansion of the shell variable DIR_WORK expands to?

Can that line appear more than once in a file?

Can anything else appear on that line? (It doesn't look like you're trying to only match lines that exactly match that string; it looks like you're only trying to match lines that contain that string possibly with other text before and/or after what is in your search string?)

When that line is found in a file, do you want to add two lines to thee end of that file? Or do you want to add those two lines immediately following the line you found?

By calling the variable you're searching for lastLine are you indicating that the line you're looking for should only appear as the last line in that file?
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 02-03-2019
Quote:
Originally Posted by Don Cragun
...
Can that line appear more than once in a file?
...
This is a very interesting formulation of the problem! Thanks
suppose this is a real string:
Code:
l='${DIR_WORK}\/loadbalancer\/html:\/var\/www\/html'

Only after the first match:
Code:
sed -i '0,/'"$l"'/ {/'"$l"'/ a \extra_hosts:'$'\n}' testfile

or
Code:
sed -ni 's/'"$l"'/&\nexra_hosts:/;p;T;:1;n;p;b1' testfile

For example, only after the second match
Code:
sed '/'"$l"'/! b;x;s/$/\n/;/^\n\n$/! b1;x;s/.*/&\nextra_host:/;b;:1;x'

or after the third match:
Code:
sed '/'"$l"'/! b;x;s/$/\n/;/^\n\n\n$/! b1;x;s/.*/&\nextra_hosts:/;b;:1;x'

gnu bash 4.4 redhat-linux
# 5  
Old 02-03-2019
Hi nezabudka,
There are lots of possibilities here. Why don't we wait for siamak to explain what is really wanted.

It is too late at night (actually early in the morning) for me to come up with solutions for all of the possibilities that might meet siamak's ambiguous requirements.

Cheers,
Don

Last edited by Don Cragun; 02-03-2019 at 01:54 PM.. Reason: Remove obvious quote.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

3. Shell Programming and Scripting

Insert New Line Character

Hi, If my first character of a line starts with 2 then after 5th charecter newline character should be inserted. Input.txt: a1234567890 2222300007 bsdfsdf888999999 ssdfkjskfdjskfdjd 2899900000000099999999999999 28887777 999999999999999999 Output.txt: a1234567890 22223 00007... (8 Replies)
Discussion started by: unme
8 Replies

4. Solaris

Insert a file at specific line

Hi, Anyone can help me in Solaris command on how to insert a file at specific line. I want file1.sql content to be inserted on file2.sh after "recover database using backup controlfile until cancel". # file1.sql /archivelogs/927_822338133.arc /archivelogs/671_822338107.arc... (3 Replies)
Discussion started by: fspalero
3 Replies

5. Shell Programming and Scripting

Insert a character before a line

I have a file and I can get the line with a specific pattern. I want to inset # on start of the line. file.text ==== aa bb cc bb hh kk kk ll yy dd aa kk rr tt aa I want to comment out the line with contain "aa" after running the script file.text ==== #aa bb cc bb hh kk kk ll... (7 Replies)
Discussion started by: Biplab
7 Replies

6. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

7. Shell Programming and Scripting

Replace, insert n times a specific character

How can using Vim, replace one character with another repeating it 10 times? Ex.: Transforming this: 125A986 That: 125##########986 (12 Replies)
Discussion started by: IJNeves
12 Replies

8. UNIX for Dummies Questions & Answers

Insert character in the line

Hi All, I have below type file. abc|asd|pqr|2|2|2 asc|qwe|scf|5|4|4 Pipe location and count is dynamic and coming from a variable. I want to change it to below files. (chnage the first pipe to 3 pipes) abc|||asd|pqr|2|2|2 asc|||qwe|scf|5|4|4 (chnage the second pipe to 4 pipes)... (1 Reply)
Discussion started by: swat
1 Replies

9. Shell Programming and Scripting

How to insert a character in line in Special condition?

Hi, I have a log file generated by a tool which has the following look : /tmp/releases/directory/datefilename1_release_date.zip /tmp/releases/directory/datefilename2_release_date.zip /tmp/releases/directory/datefilename3_release_date.zip... (8 Replies)
Discussion started by: bhaskar_m
8 Replies

10. Shell Programming and Scripting

awk insert character in the middle of a line

I'm trying to insert a single character at position 11 in everyline of a file. My input file looks like this: 456781 ~Y~12345 456782 ~N~12300 and I want my output to look like this: 45678~1 ~Y~12345 45678~2 ~N~12300 I tried the following awk code, but it's not working:... (3 Replies)
Discussion started by: mmarino
3 Replies
Login or Register to Ask a Question