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
# 15  
Old 05-13-2014
Quote:
Originally Posted by protocomm
an other solution:

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

This is extremely dangerous.

First, I assume you mean awk command in the generic sense, rather than command awk literally (which does something entirely different).

If the awk script fails for any reason, the contents of file will be destroyed. Depending on the operating system and shell you're using, echo "$var" may modify the contents of file if the expansion of $var contains any backslash (\) characters or starts with a minus-sign (-) character.
This User Gave Thanks to Don Cragun For This Post:
# 16  
Old 05-13-2014
Quote:
Originally Posted by Don Cragun
This is extremely dangerous.

First, I assume you mean awk command in the generic sense, rather than command awk literally (which does something entirely different).

If the awk script fails for any reason, the contents of file will be destroyed. Depending on the operating system and shell you're using, echo "$var" may modify the contents of file if the expansion of $var contains any backslash (\) characters or starts with a minus-sign (-) character.
thanks for your explications, i'm not an expert as you, i use always the same OS (OSX) and the same shell (bash 3.2), in my case i think that $var don't modify the contents of file

message to MadeInGermany
I never work with bigger file text.

Thx
# 17  
Old 05-13-2014
Quote:
Originally Posted by protocomm
thanks for your explications, i'm not an expert as you, i use always the same OS (OSX) and the same shell (bash 3.2), in my case i think that $var don't modify the contents of file

... ... ...

Thx
Hi protocomm,
Try the following script:
Code:
var=$(awk '1' < file);echo "$var" > file

on OS X using bash when file contains:
Code:
-n

I think you will find that it changes the contents from the three characters -, n, and <newline> to an empty file.

On some other operating systems, much more common file contents could be converted in ways (some obvious and some subtle) that would render working shell scripts or correct C source code contained in file into streams of syntax errors.
# 18  
Old 05-13-2014
Quote:
Originally Posted by Don Cragun
Hi protocomm,
Try the following script:
Code:
var=$(awk '1' < file);echo "$var" > file

on OS X using bash when file contains:
Code:
-n

I think you will find that it changes the contents from the three characters -, n, and <newline> to an empty file.

On some other operating systems, much more common file contents could be converted in ways (some obvious and some subtle) that would render working shell scripts or correct C source code contained in file into streams of syntax errors.
I try your line code and after the treatment and put
Code:
-n

in the file, the contents not change, the file is the same that before
# 19  
Old 05-13-2014
Interesting...
Code:
bash-3.2$ bash --version
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.
bash-3.2$ printf -- "-n\n" > file
bash-3.2$ ls -l file
-rw-r--r--  1 dwc  staff  3 May 13 09:45 file
bash-3.2$ var=$(awk '1' < file);echo "$var" > file
bash-3.2$ ls -l file
-rw-r--r--  1 dwc  staff  0 May 13 09:46 file
bash-3.2$

# 20  
Old 05-13-2014
i have the same result that you demonstrate…
But if i put something after the -n\n , the file is not empty…

In fact i don't understand that you want to explain for me, sorry…, i don't understand the interest…
In which case i will have a file just with -n\n as content ?
# 21  
Old 05-13-2014
Quote:
Originally Posted by protocomm
i have the same result that you demonstrate...
But if i put something after the -n\n , the file is not empty...

In fact i don't understand that you want to explain for me, sorry..., i don't understand the interest...
In which case i will have a file just with -n\n as content ?
The point is that in message #13 in this thread, you implied that
Code:
cp filename file.backup && \
awk 'awk_commands' file.backup > file && \
rm filename.backup
        and
var=$(awk 'awk_commands' < file);echo "$var" > file

are equivalent.

I want the people with less experience reading this thread to understand that although they may be equivalent in some cases on some systems; in cases of resource shortages, I/O errors, or power failures and (depending on the size of file, the contents of the file, and the version of echo you're using), some or all of the contents of file may be destroyed by the code shown in red while the code shown in green would preserve the original contents of file or atomically replace the contents of file with the results of any changes made by the awk script

(And, yes, I know that undetected hardware errors could destroy the contents of file with either method. But, we all hope that we don't have to worry about those cases too often.)
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. 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