sed and awk to insert a line after a para


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed and awk to insert a line after a para
# 1  
Old 05-12-2014
Tools sed and awk to insert a line after a para

hi

I am having a file like this

##############################
Code:
mod1 ( a(ll) , b(
c), try(o) , oll(ll)
go(oo) , al(ll)
mm(al) , lpo(kka)
kka(oop) );

mod2 ( jj(ll) , c(
kk), try1q(o1) , ofll(lll)
gao(oo1) , ala(llaa)
mmf(adl) , lddpo(kkad)
kkda(oodp) );

##########################################

I want to match the regular expression line mod1 and mod2 in the file and insert a line

Code:
#####################
ak(kk) , akjj(ll) , aa(99k)

after the module ending before ));

Output file

Code:
mod1 ( a(ll) , b(
c), try(o) , oll(ll)
go(oo) , al(ll)
mm(al) , lpo(kka)
kka(oop) ,ak(kk) , akjj(ll) , aa(99k) );

mod2 ( jj(ll) , c(
kk), try1q(o1) , ofll(lll)
gao(oo1) , ala(llaa)
mmf(adl) , lddpo(kkad)
kkda(oodp) ,ak(kk) , akjj(ll) , aa(99k) );

##############################

I am using below command
Code:
sed '/mod1/a ak(kk) , akjj(ll) , aa(99k)' file1 
sed '/mod2/a ak(kk) , akjj(ll) , aa(99k)' file1

but it is appending text after match only

Please let me know how it can work

Last edited by Don Cragun; 05-12-2014 at 04:12 AM.. Reason: Change HTML tags to CODE tags.
# 2  
Old 05-12-2014
Code:
sed 's/;/ak(kk) , akjj(ll) , aa(99k);/g'

# 3  
Old 05-12-2014
regular expression match and insert line before ending the module

Thanks Protocomm

But this is a small section of the file
so I need to match with mod1 and mod2 regular expression and insert the line where those modules are ending before ));

Thanks
Kshitij
# 4  
Old 05-12-2014
Try:
Code:
awk '
$1 ~ /^mod[12]$/ {
	add = 1
}
add && /[)];/ {
	sub(/[)];/, ",ak(kk) , akjj(ll) , aa(99k) );")
	add = 0
}
1' file

If you want to use this on a Solaris/SunOS system, use /usr/xpg4/bin/awk or /usr/xpg6/bin/awk instead of just awk.

Last edited by Don Cragun; 05-12-2014 at 04:28 AM.. Reason: Simplify the awk script.
# 5  
Old 05-12-2014
Maybe you can prepend the stuff?
Code:
sed '
/mod1 (/ s//& ak(kk) , akjj(ll) , aa(99k) ,/
/mod2 (/ s//& ak(kk) , akjj(ll) , aa(99k) ,/
' file1

# 6  
Old 05-12-2014
Quote:
Originally Posted by Don Cragun
Try:
Code:
awk '
$1 ~ /^mod[12]$/ {
	add = 1
}
add && /[)];/ {
	sub(/[)];/, ",ak(kk) , akjj(ll) , aa(99k) );")
	add = 0
}
1' file

If you want to use this on a Solaris/SunOS system, use /usr/xpg4/bin/awk or /usr/xpg6/bin/awk instead of just awk.
Don Cragun,
I don't understand the working of the variable add ???
Could you explain please ?
if add is equal to 1 then you do sub ?
but why 1 && /[)];/ allow it ?

Thanx
# 7  
Old 05-12-2014
Probably you understand, if it becomes more explicit
Code:
( add==1 && /[)];/ ) { ... }

or
Code:
{ if ( add==1 && /[)];/ ) { ... }
}

NB && means AND
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

sed insert into line 1 via script

Hi I am trying to run a sed command within a script..unfortunately it wasn't written on Solaris so doesn't work. Can anyone help with the correct coding please? It is: sed -i '1i ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE' ${OUTFILE} (4 Replies)
Discussion started by: Grueben
4 Replies

2. Shell Programming and Scripting

How to extract entire para instead of just line?

Hello, I have a file with multiple paragraphs/sections each starting with word "Handle" and if I grep for a pattern, I should get contents of entire section/para (not just line). Please advise, thanks! #script.sh file.txt "System Information" Handle 0x0001 DMI type 1, 27 bytes. ... (9 Replies)
Discussion started by: reddyr
9 Replies

3. Shell Programming and Scripting

Insert a value in a pipe delimited line (unsig sed,awk)

Hi, I want to insert a value (x) in the 3rd position of each line in a file like below a|b|c|d|1 a|b|c|d a|b|c|d|e|1 a|b|cso that output file looks like a|b|x|c|d|1 a|b|x|c|d a|b|x|c|d|e|1 a|b|x|cI can do that using perl as below #!/usr/bin/perl -w use strict; #inserting x at... (5 Replies)
Discussion started by: sam05121988
5 Replies

4. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

5. Shell Programming and Scripting

Insert @Line # - SED (non-GNU)

Just posted on another fellow's question using ed, but I wanted to know about doing it with sed on Unix. For example - I have a file of an unknown length, but I want to add a line after the shell declaration (Line 2). If possible, I'd like the example to be able to just substitute in a number and... (2 Replies)
Discussion started by: Vryali
2 Replies

6. UNIX for Advanced & Expert Users

Not able to insert new line using sed in Sun Solaris

Hi Guys, can any one know how to replace the string by newline using sed ? i dont need any other solution. I am very curious to know this in sed. Input: one,two,three Output one two three we can acheive this by awk but in sed ?, awk '{gsub(/\,/,"\n");print;}' File (6 Replies)
Discussion started by: rangarasan
6 Replies

7. AIX

insert a line with variables using sed

Hi, I have to insert a line having variables using sed. But the variables are not getting substituted within sed. Ex: n=2 sed $n' i\ hi' file This works. But the below code does not work. n=2 line=hello sed $n' i\ $line' file The above code inserts '$line' in the 2nd line of the... (9 Replies)
Discussion started by: sugan
9 Replies

8. Shell Programming and Scripting

sed insert text at particular line

I know that sed -n '12p' file will print line 12 but how might I insert text to a specified line? thanks (2 Replies)
Discussion started by: action_owl
2 Replies

9. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

10. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies
Login or Register to Ask a Question