Kusalananda's answer with sed is good; I'll propose an alternative -- an in-place edit with ed
, which is installed by default on AIX:
ed -s input <<< $'1,$s!^!/home/info/!\nw\nq'
or more concisely, using ,
as a shortcut for the 1,$
entire-file address range:
ed -s input <<< $',s!^!/home/info/!\nw\nq'
The idea is otherwise the same as in sed -- use the s
earch and replace command with a delimiter that is not present in the search or replacement text, to avoid LTS; instead of #
I've arbitrarily chosen !
. The remaining commands are separated by newlines and instruct ed
to w
rite the file back out and to q
uit out. The commands are all sent as a here-string to the ed
command. I've used the -s
flag to put ed
into "script mode", where it quiets the output (bytes read and written, here).
To use here-strings, you'll need to use ksh93, also present on stock AIX systems.
Alternatively to here-strings, use printf
:
printf '1,$s!^!/home/info/!
w
q
' | ed -s input
Where I've broken the ed
commands up to multiple lines for legibility, or all in one line:
printf '1,$s!^!/home/info/!\nw\nq\n' | ed -s input
Alternatively to printf, a here-document works as well:
ed -s input << 'EOF'
1,$s!^!/home/info/!
w
q
EOF
illegal option -- i Usage: sed [-n] [-u] Script [File ...] sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
– Tarek Sayed Nov 06 '18 at 11:03cp file file.new && sed ... file.new > file && rm file.new
– Jeff Schaller Nov 06 '18 at 13:41