0

I have a file that has some lines like:

account_1
account_2 
account_3

I have to add a path like /home/info/ before each line and save the same file with those updates like this:

/home/info/account_1
/home/info/account_2 
/home/info/account_3

How can I handle this on AIX?

3 Answers3

3

To add any string to the start of each line in a file, you may use

sed -i 's#^#/home/info/#' file

This would do an in-place edit of the file called file and would insert the string /home/info/ at the start of each line.

On FreeBSD or derivatives (like macOS), you would have to use -i '' instead of -i (see also "How can I achieve portability with sed -i (in-place editing)?").

The # are used as delimiters of the sed expression here since the replacement text contains /. We could also have written

sed -i 's/^/\/home\/info\//' file

but that's a bit difficult to both read and write (see Wikipedia: Leaning toothpick syndrome).

As always, run sed without the -i option first to make sure that it does the correct thing.


For a systems where sed does not support -i for in-place editing, you will have to write the result to a temporary file and then replace the original with that file:

sed 's#^#/home/info/#' <file >file.new &&
mv file.new file

This would replace the original file with the modified file if the sed command was successful.

If you need to retain meta data such as ownership and file permissions, do it like

cp file file.tmp &&
sed 's#^#/home/info/#' <file.tmp >file &&
rm file.tmp
Kusalananda
  • 333,661
2

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 search 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 write the file back out and to quit 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
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
-1

outra alternativa com awk

awk -i inplace  '{ print  "/home/info/"  $1 }'  file

/home/info/account_1
  • -i inplace
    The inplace extension emulates GNU sed’s -i option, which performs “in-place” editing of each input file. It uses the bundled inplace.awk include file to invoke the extension properly:

  • print "/home/info/" concatenates the first field.

  • $1 represents the first field of the file.

Archemar
  • 31,554
noob
  • 22