Get Substring assigned to a variable

Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
bash-4.1$ ps -ef|grep component_events.xml
gdxx1122 1190 1181 0 Mar25 ? 00:00:00 /xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs -l -h:/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1/component_events.xml_ohs1
/xx1122/admin/xx1122_vmabcd123/auditlogs/OHS/ohs1/audit-pid1181-%Y%m%d%H%M%S.log 1M 4M
gdxx1122 19885 19205 0 08:05 pts/1 00:00:00 grep component_events.xml
When i run the above command in a script, can extract "/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1" and assigned to a variable. The output of above may vary server to server, so it has to be dynamic. Starting string xx1122 has to be passed dynamically
linux shell
add a comment |Â
up vote
3
down vote
favorite
bash-4.1$ ps -ef|grep component_events.xml
gdxx1122 1190 1181 0 Mar25 ? 00:00:00 /xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs -l -h:/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1/component_events.xml_ohs1
/xx1122/admin/xx1122_vmabcd123/auditlogs/OHS/ohs1/audit-pid1181-%Y%m%d%H%M%S.log 1M 4M
gdxx1122 19885 19205 0 08:05 pts/1 00:00:00 grep component_events.xml
When i run the above command in a script, can extract "/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1" and assigned to a variable. The output of above may vary server to server, so it has to be dynamic. Starting string xx1122 has to be passed dynamically
linux shell
you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
â lese
Jun 15 at 7:37
You want the logging directory? Is there no better way of getting than than from thepsoutput?
â Kusalananda
Jun 15 at 7:41
I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
â Gayathri Gowda
Jun 15 at 8:55
Do you know beforehand what the backup directories are called? You could uselocatetogether withgrep. That would work even if the process isn't running.
â Kusalananda
Jun 15 at 13:48
i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
â Gayathri Gowda
Jun 15 at 17:22
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
bash-4.1$ ps -ef|grep component_events.xml
gdxx1122 1190 1181 0 Mar25 ? 00:00:00 /xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs -l -h:/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1/component_events.xml_ohs1
/xx1122/admin/xx1122_vmabcd123/auditlogs/OHS/ohs1/audit-pid1181-%Y%m%d%H%M%S.log 1M 4M
gdxx1122 19885 19205 0 08:05 pts/1 00:00:00 grep component_events.xml
When i run the above command in a script, can extract "/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1" and assigned to a variable. The output of above may vary server to server, so it has to be dynamic. Starting string xx1122 has to be passed dynamically
linux shell
bash-4.1$ ps -ef|grep component_events.xml
gdxx1122 1190 1181 0 Mar25 ? 00:00:00 /xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs -l -h:/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1/component_events.xml_ohs1
/xx1122/admin/xx1122_vmabcd123/auditlogs/OHS/ohs1/audit-pid1181-%Y%m%d%H%M%S.log 1M 4M
gdxx1122 19885 19205 0 08:05 pts/1 00:00:00 grep component_events.xml
When i run the above command in a script, can extract "/xx1122/admin/xx1122_vmabcd123/config/OHS/ohs1" and assigned to a variable. The output of above may vary server to server, so it has to be dynamic. Starting string xx1122 has to be passed dynamically
linux shell
edited Jun 15 at 8:41
asked Jun 15 at 7:25
Gayathri Gowda
203
203
you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
â lese
Jun 15 at 7:37
You want the logging directory? Is there no better way of getting than than from thepsoutput?
â Kusalananda
Jun 15 at 7:41
I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
â Gayathri Gowda
Jun 15 at 8:55
Do you know beforehand what the backup directories are called? You could uselocatetogether withgrep. That would work even if the process isn't running.
â Kusalananda
Jun 15 at 13:48
i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
â Gayathri Gowda
Jun 15 at 17:22
add a comment |Â
you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
â lese
Jun 15 at 7:37
You want the logging directory? Is there no better way of getting than than from thepsoutput?
â Kusalananda
Jun 15 at 7:41
I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
â Gayathri Gowda
Jun 15 at 8:55
Do you know beforehand what the backup directories are called? You could uselocatetogether withgrep. That would work even if the process isn't running.
â Kusalananda
Jun 15 at 13:48
i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
â Gayathri Gowda
Jun 15 at 17:22
you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
â lese
Jun 15 at 7:37
you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
â lese
Jun 15 at 7:37
You want the logging directory? Is there no better way of getting than than from the
ps output?â Kusalananda
Jun 15 at 7:41
You want the logging directory? Is there no better way of getting than than from the
ps output?â Kusalananda
Jun 15 at 7:41
I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
â Gayathri Gowda
Jun 15 at 8:55
I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
â Gayathri Gowda
Jun 15 at 8:55
Do you know beforehand what the backup directories are called? You could use
locate together with grep. That would work even if the process isn't running.â Kusalananda
Jun 15 at 13:48
Do you know beforehand what the backup directories are called? You could use
locate together with grep. That would work even if the process isn't running.â Kusalananda
Jun 15 at 13:48
i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
â Gayathri Gowda
Jun 15 at 17:22
i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
â Gayathri Gowda
Jun 15 at 17:22
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
With sed, POSIXly:
var=$(
ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
)
In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.
So if the command line was:
... -h:x/component_events.xml -g:y/component_events.xml
You'd get x/components_events.xml -g:y instead of x.
You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.
On a recent GNU/Linux system, you could also do:
LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
tr '' 'n'
Which would work for any directory name.
@RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support-Pcombined with-z. With your version,Amight work better than^. See edit.
â Stéphane Chazelas
Jun 15 at 11:55
1
@RoVo, and you have a process with a-h:.../component_events.xml...in its list of arguments?
â Stéphane Chazelas
Jun 15 at 12:33
no ^^ and this might be the problem ;-)
â RoVo
Jun 15 at 12:37
1
@RoVo, It seems the version of grep on 16.04 has that bug
â Stéphane Chazelas
Jun 15 at 12:40
@RoVo, I've reverted to^asAwon't help with those brokengrepversions.
â Stéphane Chazelas
Jun 15 at 13:18
add a comment |Â
up vote
0
down vote
Using pgrep to find the pid and /proc/$PID/cmdline:
var=$(
pgrep -f "component_events.xml" |
xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
xargs -n1 dirname
)
This also works when the command path has arguments and when multiple PIDs were found.
Explanation:
Search for PIDs:
pgrep -f "component_events.xml"
For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).
xargs -I sh -c "head -z -n1 /proc//cmdline
Then replace trailing with newline (n):
tr '' 'n'"
Output the dirname only:
xargs -n1 dirname
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With sed, POSIXly:
var=$(
ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
)
In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.
So if the command line was:
... -h:x/component_events.xml -g:y/component_events.xml
You'd get x/components_events.xml -g:y instead of x.
You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.
On a recent GNU/Linux system, you could also do:
LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
tr '' 'n'
Which would work for any directory name.
@RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support-Pcombined with-z. With your version,Amight work better than^. See edit.
â Stéphane Chazelas
Jun 15 at 11:55
1
@RoVo, and you have a process with a-h:.../component_events.xml...in its list of arguments?
â Stéphane Chazelas
Jun 15 at 12:33
no ^^ and this might be the problem ;-)
â RoVo
Jun 15 at 12:37
1
@RoVo, It seems the version of grep on 16.04 has that bug
â Stéphane Chazelas
Jun 15 at 12:40
@RoVo, I've reverted to^asAwon't help with those brokengrepversions.
â Stéphane Chazelas
Jun 15 at 13:18
add a comment |Â
up vote
3
down vote
accepted
With sed, POSIXly:
var=$(
ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
)
In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.
So if the command line was:
... -h:x/component_events.xml -g:y/component_events.xml
You'd get x/components_events.xml -g:y instead of x.
You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.
On a recent GNU/Linux system, you could also do:
LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
tr '' 'n'
Which would work for any directory name.
@RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support-Pcombined with-z. With your version,Amight work better than^. See edit.
â Stéphane Chazelas
Jun 15 at 11:55
1
@RoVo, and you have a process with a-h:.../component_events.xml...in its list of arguments?
â Stéphane Chazelas
Jun 15 at 12:33
no ^^ and this might be the problem ;-)
â RoVo
Jun 15 at 12:37
1
@RoVo, It seems the version of grep on 16.04 has that bug
â Stéphane Chazelas
Jun 15 at 12:40
@RoVo, I've reverted to^asAwon't help with those brokengrepversions.
â Stéphane Chazelas
Jun 15 at 13:18
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With sed, POSIXly:
var=$(
ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
)
In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.
So if the command line was:
... -h:x/component_events.xml -g:y/component_events.xml
You'd get x/components_events.xml -g:y instead of x.
You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.
On a recent GNU/Linux system, you could also do:
LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
tr '' 'n'
Which would work for any directory name.
With sed, POSIXly:
var=$(
ps -Ao args= | sed -n 's|.* -h:(.*)/component_events.xml.*|1|p'
)
In effect, that returns the part of the command line in between the last occurrence of /component_events.xml and the last occurrence before that of -h:.
So if the command line was:
... -h:x/component_events.xml -g:y/component_events.xml
You'd get x/components_events.xml -g:y instead of x.
You could change it to sed -n 's|.* -h:([^ ]*)/component_events.xml.*|1|p' to avoid that, but then that would no longer for directory names that contain space characters.
On a recent GNU/Linux system, you could also do:
LC_ALL=C grep -zhPo '^-h:K.*(?=/component_events.xml)' /proc/*/cmdline |
tr '' 'n'
Which would work for any directory name.
edited Jun 15 at 13:18
answered Jun 15 at 7:42
Stéphane Chazelas
279k53513844
279k53513844
@RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support-Pcombined with-z. With your version,Amight work better than^. See edit.
â Stéphane Chazelas
Jun 15 at 11:55
1
@RoVo, and you have a process with a-h:.../component_events.xml...in its list of arguments?
â Stéphane Chazelas
Jun 15 at 12:33
no ^^ and this might be the problem ;-)
â RoVo
Jun 15 at 12:37
1
@RoVo, It seems the version of grep on 16.04 has that bug
â Stéphane Chazelas
Jun 15 at 12:40
@RoVo, I've reverted to^asAwon't help with those brokengrepversions.
â Stéphane Chazelas
Jun 15 at 13:18
add a comment |Â
@RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support-Pcombined with-z. With your version,Amight work better than^. See edit.
â Stéphane Chazelas
Jun 15 at 11:55
1
@RoVo, and you have a process with a-h:.../component_events.xml...in its list of arguments?
â Stéphane Chazelas
Jun 15 at 12:33
no ^^ and this might be the problem ;-)
â RoVo
Jun 15 at 12:37
1
@RoVo, It seems the version of grep on 16.04 has that bug
â Stéphane Chazelas
Jun 15 at 12:40
@RoVo, I've reverted to^asAwon't help with those brokengrepversions.
â Stéphane Chazelas
Jun 15 at 13:18
@RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support
-P combined with -z. With your version, A might work better than ^. See edit.â Stéphane Chazelas
Jun 15 at 11:55
@RoVo, I can't explain your first problem. For your second, yes some versions of GNU grep have had this kind of problem. Some didn't even support
-P combined with -z. With your version, A might work better than ^. See edit.â Stéphane Chazelas
Jun 15 at 11:55
1
1
@RoVo, and you have a process with a
-h:.../component_events.xml... in its list of arguments?â Stéphane Chazelas
Jun 15 at 12:33
@RoVo, and you have a process with a
-h:.../component_events.xml... in its list of arguments?â Stéphane Chazelas
Jun 15 at 12:33
no ^^ and this might be the problem ;-)
â RoVo
Jun 15 at 12:37
no ^^ and this might be the problem ;-)
â RoVo
Jun 15 at 12:37
1
1
@RoVo, It seems the version of grep on 16.04 has that bug
â Stéphane Chazelas
Jun 15 at 12:40
@RoVo, It seems the version of grep on 16.04 has that bug
â Stéphane Chazelas
Jun 15 at 12:40
@RoVo, I've reverted to
^ as A won't help with those broken grep versions.â Stéphane Chazelas
Jun 15 at 13:18
@RoVo, I've reverted to
^ as A won't help with those broken grep versions.â Stéphane Chazelas
Jun 15 at 13:18
add a comment |Â
up vote
0
down vote
Using pgrep to find the pid and /proc/$PID/cmdline:
var=$(
pgrep -f "component_events.xml" |
xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
xargs -n1 dirname
)
This also works when the command path has arguments and when multiple PIDs were found.
Explanation:
Search for PIDs:
pgrep -f "component_events.xml"
For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).
xargs -I sh -c "head -z -n1 /proc//cmdline
Then replace trailing with newline (n):
tr '' 'n'"
Output the dirname only:
xargs -n1 dirname
add a comment |Â
up vote
0
down vote
Using pgrep to find the pid and /proc/$PID/cmdline:
var=$(
pgrep -f "component_events.xml" |
xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
xargs -n1 dirname
)
This also works when the command path has arguments and when multiple PIDs were found.
Explanation:
Search for PIDs:
pgrep -f "component_events.xml"
For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).
xargs -I sh -c "head -z -n1 /proc//cmdline
Then replace trailing with newline (n):
tr '' 'n'"
Output the dirname only:
xargs -n1 dirname
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using pgrep to find the pid and /proc/$PID/cmdline:
var=$(
pgrep -f "component_events.xml" |
xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
xargs -n1 dirname
)
This also works when the command path has arguments and when multiple PIDs were found.
Explanation:
Search for PIDs:
pgrep -f "component_events.xml"
For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).
xargs -I sh -c "head -z -n1 /proc//cmdline
Then replace trailing with newline (n):
tr '' 'n'"
Output the dirname only:
xargs -n1 dirname
Using pgrep to find the pid and /proc/$PID/cmdline:
var=$(
pgrep -f "component_events.xml" |
xargs -I sh -c "head -z -n1 /proc//cmdline | tr '' 'n'" |
xargs -n1 dirname
)
This also works when the command path has arguments and when multiple PIDs were found.
Explanation:
Search for PIDs:
pgrep -f "component_events.xml"
For each found PID, get the full command (/proc/PID/cmdline), but print only the command without args (head -z -n1).
xargs -I sh -c "head -z -n1 /proc//cmdline
Then replace trailing with newline (n):
tr '' 'n'"
Output the dirname only:
xargs -n1 dirname
edited Jun 15 at 8:35
answered Jun 15 at 8:16
RoVo
83219
83219
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449943%2fget-substring-assigned-to-a-variable%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
you want the whole string "/xx1122/fmw/product/111/ohs_111/ohs/bin/odl_rotatelogs" or it must be trim after 'ohs1' ? If must be trim, 'ohs1' is constant?
â lese
Jun 15 at 7:37
You want the logging directory? Is there no better way of getting than than from the
psoutput?â Kusalananda
Jun 15 at 7:41
I am looking for the directory where the file "component_events.xml" is found .. the reason i am taking it through ps is .. the same file is found in back up locations as well
â Gayathri Gowda
Jun 15 at 8:55
Do you know beforehand what the backup directories are called? You could use
locatetogether withgrep. That would work even if the process isn't running.â Kusalananda
Jun 15 at 13:48
i will not know because this script is going to run on many servers. so it will be quite difficult to find the back up directories. That is the reason i am trying to find the file associated with the process
â Gayathri Gowda
Jun 15 at 17:22