Because xargs puts the line it is processing where you put the '{}'. You put it after the filename. Try
su - db2inst1 -c "db2 get dbm cfg | grep DIAGPATH | grep -v ALT_DIAGPATH" |
awk '{print $7}'| xargs -I '{}' tail -20 '{}'/db2diag.log
Question
Below is the path that I am parsing. Keep in mind that DIAGPATH is changing, so I don't want to hard code it.
DIAGPATH=/home/db2inst1/sqllib/db2dump # Diagnostic data directory path
Under that directory there are files whose last 20 lines I want to read. One of the file is called db2diag.log.
Here is the command I have, but it is not working.
su - db2inst1 -c "db2 get dbm cfg | grep DIAGPATH | grep -v ALT_DIAGPATH" |
awk '{print $7}'| xargs -I '{}' tail -20 db2diag.log'{}'
Here is the output from the above command:
db2diag.log/home/db2inst1/sqllib/db2dump: A file or directory in the path name does not exist.
I am not sure why it is putting "db2diag.log" at the beginning. It should look like this and it should give the last 20 lines of the output:
tail -20 /home/db2inst1/sqllib/db2dump/db2diag.log
Solution
Because xargs puts the line it is processing where you put the '{}'. You put it after the filename. Try
su - db2inst1 -c "db2 get dbm cfg | grep DIAGPATH | grep -v ALT_DIAGPATH" |
awk '{print $7}'| xargs -I '{}' tail -20 '{}'/db2diag.log