Add a line after last matched expression


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Add a line after last matched expression
# 8  
Old 08-26-2012
Code:
sed '${/^ACL localnet/{s/.*/&\nMy Network 192.168.1.1/}}' infile

# 9  
Old 08-26-2012
Quote:
Originally Posted by complex.invoke
Code:
sed '${/^ACL localnet/{s/.*/&\nMy Network 192.168.1.1/}}' infile

This will work only if the last line of the file begins with "ACL localnet". Smilie
But in this case an echo "My Network 192.168.1.1" >> file would be enough.

The OP wants to insert a line after the last occurrence, in the file, of a line beginning with "ACL localnet". This last occurrence may be everywhere in the file.

The solution I posted above works well (I wrote it for bash), but I suggested not to use it because of the trick of deleting the file before rewriting it and having it only in RAM for a while, which would end up in a loss of data in case of a system crash.

However:
Code:
lem@biggy:/tmp$ line=$(sed -n '/^ACL localnet/ =' file |tail -n1)
lem@biggy:/tmp$ (( ${line:-0} == 0 )) || sed  "$line a\
My Network 192.168.1.1" <file >newfile

(after "$line a\" press ENTER)
is perfectly safe. Smilie
--
Bye
# 10  
Old 08-26-2012
Any one liners?
# 11  
Old 08-26-2012
Quote:
Originally Posted by glev2005
Any one liners?
Given what it is you want to do, I'd say not.

Why is it important to have it all smashed on one line?

Most anything can be written as a one liner, but IMHO they become unmanageable very quickly which opens them up for errors.

Here is the suggestion I posted earlier, as a one (huge) liner:

Code:
 function print_cache( rec ) { if( ! buffer ) return; close( cache ); while( (getline rec <cache ) > 0 ) print rec; close( cache ); } /^ACL localnet/ { print_cache(); print; buffer = 1; next; } { if( buffer ) print >cache; else print; next; } END { if( buffer ) print "My Network 192.168.1.1 "; print_cache( ); system( "rm " cache ); } ' input >output

# 12  
Old 08-26-2012
I'm not sure this will work under every circumstance, but it's a one liner, give it a try:
Code:
sed "$(awk '/^ACL localnet/ {n=NR} END{print  n}' infile)  a \My Network 192.168.1.1" infile
|                                                            ^---  edited after Don Cragun's below post


Last edited by RudiC; 08-26-2012 at 02:11 PM..
# 13  
Old 08-26-2012
Quote:
Originally Posted by RudiC
I'm not sure this will work under every circumstance, but it's a one liner, give it a try:
Code:
sed "$(awk '/^ACL localnet/ {n=NR} END{print  n}' infile)  a My Network 192.168.1.1" infile

On OS X this yields:
Code:
sed: 1: "14  a My Network 192.16 ...": command a expects \ followed by text

# 14  
Old 08-26-2012
Put a \ between the "a" and "My ..." - it worked without on my linux but the standard is "a \"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

3. Shell Programming and Scripting

How can I get the matched text when using regular expression.

Hello: (exp) : match "exp",the matched text is stored in auto named arrays. How can I get the matched text ? What is the name of the auto named arrays on linux shell ? (4 Replies)
Discussion started by: 915086731
4 Replies

4. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

5. Shell Programming and Scripting

matched characters - regular expression

Hi, I read the book of <<unix shell programming>>. The regular expression ^\(.\)\1 matches the first character on the line and stores it in register 1. Then the expression matches whatever is stored in the register 1, as specified by the \1. The net effect of this regular expression is to match... (2 Replies)
Discussion started by: jianma
2 Replies

6. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

7. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

8. Shell Programming and Scripting

Syntax error at line 11 : `for' is not matched.

hi , while running this script i am getting below error: Syntax error at line 11 : `for' is not matched. here is program: #! /bin/ksh sqlplus -s << !+! user/password update GR_SUB_SCRIPT_PC set act_del_ind='0'; commit; exit; !+! cd /home/salunke/pvcsfiles/source_files for i in... (5 Replies)
Discussion started by: digambar
5 Replies

9. Shell Programming and Scripting

How to get a next line of a matched word?

Hi , If I match a word in a file, I want to pick the next line of that matched word. My file is a below format- The ntrag trace has auditError Line5005 contains transaction Ntrag data ------------ Here I wanted if I match a word auditError, I need to get the next line "Line5005... (10 Replies)
Discussion started by: Muktesh
10 Replies

10. Shell Programming and Scripting

AWK - Extracting matched line

Hi all, I have one more query related to AWK. I have the following csv data: ,qwertyA, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200 ,,,,,,,,,,,,,,,,,,,300,400 ,qwertyB, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200... (9 Replies)
Discussion started by: not4google
9 Replies
Login or Register to Ask a Question