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
# 1  
Old 08-23-2012
Add a line after last matched expression

I thought this would be easy to Google, but I am having trouble getting a clean result that I can understand.

I simply want to insert the the line:
My Network 192.168.1.1

After the last line that begins with ACL localnet
# 2  
Old 08-23-2012
Try:
Code:
#!/bin/ksh
ed - in.addafter <<EOF
?^ACL localnet?a
My Network 192.168.1.1 
.       
w       
EOF

# 3  
Old 08-23-2012
Is ed - intended to be ed -s?

I'm not sure if "After the last line that begins with ACL localnet" means that there is only one match and that it occurs on the last line of the file, or if it means that there may be multiple matches. If the latter, I would suggest moving to line 1 before beginning the backwards search. As it is, if the final match is the last line, and if there is an earlier match, the earlier match will "win".

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 08-23-2012
Don't use it.
Code:
lem@biggy:/tmp$ line=$(sed -n '/^ACL localnet/ =' file |tail -n1)
lem@biggy:/tmp$ (( ${line:-0} == 0 )) || { rm file ; sed  "$line a\
My Network 192.168.1.1" > file ; } <file

I said don't use it.
--
Bye

Last edited by Lem; 08-23-2012 at 07:16 PM..
# 5  
Old 08-23-2012
Quote:
Originally Posted by alister
Is ed - intended to be ed -s?

I'm not sure if "After the last line that begins with ACL localnet" means that there is only one match and that it occurs on the last line of the file, or if it means that there may be multiple matches. If the latter, I would suggest moving to line 1 before beginning the backwards search. As it is, if the final match is the last line, and if there is an earlier match, the earlier match will "win".

Regards,
Alister
Yes. Thank you for catching this. I thought I typed the "s", but it isn't in my test script. Smilie

The POSIX standard says that a "-" operand produces unspecified results. On some systems, "-" is a synonym for "-s"; on other systems "-" is a file operand referring to ed's standard input. On OS X (the OS on my primary machine), "-" and "-s" are synonyms so I didn't notice the typo when I was testing.

You are also correct in noting that the current line is set to the last line in the buffer when a file is loaded into the buffer by the e command, the E command, or by naming a file operand on the command line.

Note that the following script could just use the command 1 (digit one; not letter el), but that would print the first line. Substituting the start of line 1 to nothing doesn't change anything on the line, but changes the current line to the 1st line in the buffer without printing anything.
Code:
#!/bin/ksh
ed - in.addafter <<EOF
1s/^//
?^ACL localnet?a
My Network 192.168.1.1
.
w
q
EOF

Note that although I specified /bin/ksh for this script, any shell should work. I tested it with ksh, bash, csh, sh, and tcsh.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-25-2012
I mean that there are multiple lines that begin with ACL localnet and I want to insert a line after the last instance.
# 7  
Old 08-25-2012
An alternate approach:

Code:
awk -v cache=/tmp/PID$$.cache '
    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-file >output-file

Doesn't insert the line if the pattern isn't found.

Last edited by agama; 08-25-2012 at 12:40 PM..
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