With awk
:
awk -v l="$(wc -L <file)" '{printf "%"l"s\n", $0}' file
-v
assigns an external value to an awk
variable l
.
wc -L <file
find the length of the longest line in the file.
printf "%"l"s\n", $0
prints each line space padded with by l
spaces. For 10 spaces, it would for example look like: printf "%10s\n", $0
.
The output:
7241,11111111111111111111111111111111,\01-data\file1
1237241,22222222222222222222222222222222,\01-data\file2
41,33333333333333333333333333333333,\01-data\file3
Using GNU sed
(BSD sed
will not add newlines after the added text, unless you include a literal newline in the string that is added):
sed -e '/\[httpd\]/a\' -e 'secure_rewrites = false;' \
-e '/\[couchdb\]/a\' -e 'delayed_commits = false;' local.ini
If the file contains
[httpd]
[couchdb]
initially, then the above sed
command will produce
[httpd]
secure_rewrites = false;
[couchdb]
delayed_commits = false;
Also note that the a
command for appending text is supposed to be written a\
followed by the text appended. GNU sed
is forgiving about omitting the \
.
As a sed
script (works in any sed
):
/\[httpd\]/a\
secure_rewrites = false;
/\[couchdb\]/a\
delayed_commits = false;
This could be use on the command line as a literal single quoted sed
script string (a newline has to come after the last line), or stored separately and fed into sed
using
$ sed -f script.sed local.ini
With #!/usr/bin/sed -f
, as the first line (assuming the path to sed
is correct), the script could even be run directly:
$ ./script.sed local.ini
Best Answer
You can tell
sed
to carry out multiple operations by just repeating-e
(or-f
if your script is in a file).sed -i -e 's/a/b/g' -e 's/b/d/g' file
makes both changes in the single file namedfile
, in-place. Without a backup file.sed -ibak -e 's/a/b/g' -e 's/b/d/g' file
makes both changes in the single file namedfile
, in-place. With a single backup file namedfilebak
.