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
# 8  
Old 05-12-2014
Quote:
Originally Posted by MadeInGermany
Probably you understand, if it becomes more explicit
Code:
( add==1 && /[)];/ ) { ... }

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

NB && means AND
If i understand, the AND && is an logical and then 1 && 1=1 (1 && 0=0)
This User Gave Thanks to protocomm For This Post:
# 9  
Old 05-12-2014
Here is a version of the script with comments:
Code:
awk '
$1 ~ /^mod[12]$/ {
	# If the 1st field on a line is "mod1" or "mod2", note that we need to
	# add text to the next line containing ");".
	add = 1
}
add && /[)];/ {
	# If add is not zero and is not the empty string, and the current line
	# contains ");", add in the desired text and clear add.
	sub(/[)];/, ",ak(kk) , akjj(ll) , aa(99k) );")
	add = 0
}
1	# Print the (possibly modified) input line.  ("{print $0}" is the
	# default action when no explicit action is specified.)
' file

These 2 Users Gave Thanks to Don Cragun For This Post:
# 10  
Old 05-13-2014
Wrench equivalent sed command

Thank you all !
Its working ! but is it possible to provide equivalent sed command
since I need to write to the same file with -i option of sed
I dont think with awk we can write to the same file
# 11  
Old 05-13-2014
Quote:
Originally Posted by kshitij
Thank you all !
Its working ! but is it possible to provide equivalent sed command
since I need to write to the same file with -i option of sed
I dont think with awk we can write to the same file
Redirect the output of awk to a temp file and then copy the temp file to the original file.
# 12  
Old 05-13-2014
Quote:
Originally Posted by kshitij
Thank you all !
Its working ! but is it possible to provide equivalent sed command
since I need to write to the same file with -i option of sed
I dont think with awk we can write to the same file
Using:
Code:
sed -i.backup 'sed_commands' filename

is (as chacko193 said) roughly equivalent to:
Code:
cp filename filename.backup && \
awk 'awk_commands' filename.backup > filename

if you can rewrite the simple awk script we provided into a script using sed commands to get the same results.

On the other hand:
Code:
sed -i 'sed_commands' filename

is a disaster waiting to happen. (An I/O error, lack of disk space, power failure, etc. will result in loss of data.)
# 13  
Old 05-13-2014
an other solution:

Code:
var=$(command awk < file);echo "$var" > file

# 14  
Old 05-13-2014
Quote:
Originally Posted by protocomm
an other solution:

Code:
var=$(command awk < file);echo "$var" > file

Try that on a 100GB file...
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