How to cut while Grepping [closed]
current input is from the command
grep -i "final_model" /dir1/dir2/dir3/user/dir4/assemble.preprocessing
current output is:
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
required Output-1 is:
test_F00ME001.inp
required Output-2 is:
F00ME001
centos text-processing grep
closed as unclear what you're asking by Jeff Schaller, Thomas, jimmij, Archemar, Mr Shunz Jan 30 at 15:22
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
current input is from the command
grep -i "final_model" /dir1/dir2/dir3/user/dir4/assemble.preprocessing
current output is:
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
required Output-1 is:
test_F00ME001.inp
required Output-2 is:
F00ME001
centos text-processing grep
closed as unclear what you're asking by Jeff Schaller, Thomas, jimmij, Archemar, Mr Shunz Jan 30 at 15:22
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
6
Please invest more time in writing your question. How does your input look like? How does the command you've tried looks like exactly?
– finswimmer
Jan 29 at 13:10
And where does the output go? files? variables?
– Jeff Schaller
Jan 29 at 13:26
@Jeff Schaller : problem solved. Thanks for your contribution. please remove "HOLD" from my question !!
– Nayak
Jan 31 at 14:41
@Nayak, I am unable to take your question off-hold on my own; there's a review queue dedicated to reopening questions. I personally would not vote to reopen the question yet as my question (in a comment above) has not been answered. Happy to hear you got an answer, though!
– Jeff Schaller
Jan 31 at 14:50
output is displayed on shell
– Nayak
Jan 31 at 14:53
add a comment |
current input is from the command
grep -i "final_model" /dir1/dir2/dir3/user/dir4/assemble.preprocessing
current output is:
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
required Output-1 is:
test_F00ME001.inp
required Output-2 is:
F00ME001
centos text-processing grep
current input is from the command
grep -i "final_model" /dir1/dir2/dir3/user/dir4/assemble.preprocessing
current output is:
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
required Output-1 is:
test_F00ME001.inp
required Output-2 is:
F00ME001
centos text-processing grep
centos text-processing grep
edited Jan 31 at 14:33
Kusalananda
129k16242399
129k16242399
asked Jan 29 at 13:07
NayakNayak
266
266
closed as unclear what you're asking by Jeff Schaller, Thomas, jimmij, Archemar, Mr Shunz Jan 30 at 15:22
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Jeff Schaller, Thomas, jimmij, Archemar, Mr Shunz Jan 30 at 15:22
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
6
Please invest more time in writing your question. How does your input look like? How does the command you've tried looks like exactly?
– finswimmer
Jan 29 at 13:10
And where does the output go? files? variables?
– Jeff Schaller
Jan 29 at 13:26
@Jeff Schaller : problem solved. Thanks for your contribution. please remove "HOLD" from my question !!
– Nayak
Jan 31 at 14:41
@Nayak, I am unable to take your question off-hold on my own; there's a review queue dedicated to reopening questions. I personally would not vote to reopen the question yet as my question (in a comment above) has not been answered. Happy to hear you got an answer, though!
– Jeff Schaller
Jan 31 at 14:50
output is displayed on shell
– Nayak
Jan 31 at 14:53
add a comment |
6
Please invest more time in writing your question. How does your input look like? How does the command you've tried looks like exactly?
– finswimmer
Jan 29 at 13:10
And where does the output go? files? variables?
– Jeff Schaller
Jan 29 at 13:26
@Jeff Schaller : problem solved. Thanks for your contribution. please remove "HOLD" from my question !!
– Nayak
Jan 31 at 14:41
@Nayak, I am unable to take your question off-hold on my own; there's a review queue dedicated to reopening questions. I personally would not vote to reopen the question yet as my question (in a comment above) has not been answered. Happy to hear you got an answer, though!
– Jeff Schaller
Jan 31 at 14:50
output is displayed on shell
– Nayak
Jan 31 at 14:53
6
6
Please invest more time in writing your question. How does your input look like? How does the command you've tried looks like exactly?
– finswimmer
Jan 29 at 13:10
Please invest more time in writing your question. How does your input look like? How does the command you've tried looks like exactly?
– finswimmer
Jan 29 at 13:10
And where does the output go? files? variables?
– Jeff Schaller
Jan 29 at 13:26
And where does the output go? files? variables?
– Jeff Schaller
Jan 29 at 13:26
@Jeff Schaller : problem solved. Thanks for your contribution. please remove "HOLD" from my question !!
– Nayak
Jan 31 at 14:41
@Jeff Schaller : problem solved. Thanks for your contribution. please remove "HOLD" from my question !!
– Nayak
Jan 31 at 14:41
@Nayak, I am unable to take your question off-hold on my own; there's a review queue dedicated to reopening questions. I personally would not vote to reopen the question yet as my question (in a comment above) has not been answered. Happy to hear you got an answer, though!
– Jeff Schaller
Jan 31 at 14:50
@Nayak, I am unable to take your question off-hold on my own; there's a review queue dedicated to reopening questions. I personally would not vote to reopen the question yet as my question (in a comment above) has not been answered. Happy to hear you got an answer, though!
– Jeff Schaller
Jan 31 at 14:50
output is displayed on shell
– Nayak
Jan 31 at 14:53
output is displayed on shell
– Nayak
Jan 31 at 14:53
add a comment |
4 Answers
4
active
oldest
votes
Assuming that
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
is a line of text that your grep
command is producing,
grep ... | sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
This would save the two modified lines into output1
and output2
Testing:
$ printf '%sn' 'final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>' |
sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
$ cat output1
test_F00ME001.inp
$ cat output2
F00ME001
The first sed
call operates on the original data and first removes everything up to and including the last /
character. It then removes the >
at the end.
The tee
saves this into output1
and also passes it on to the next stage of the pipeline.
The second sed
call operates on the modified data and first removes everything up to and including the first _
character. It then removes everything from the first dot onwards and redirects the result into output2
.
Thanks... output-1 works well, but Output-2 is ( model=</dir1/dir2/dir3/user/dir4/test_F00ME001 )
– Nayak
Jan 31 at 13:42
@Nayak That's because you are running sed separately on the grep output. This is not what I'm doing. I'm running the second sed on the result of the first.
– Kusalananda
Jan 31 at 13:50
i have added second command with pipe (on output of first sed). now result looks like (test_F00ME001). what if i have more than one "_" symbol ?!
– Nayak
Jan 31 at 14:20
@Nayak Well, that depends on where it's located.
– Kusalananda
Jan 31 at 14:34
now, i have combined both sed commands, then with pipe added rev and cut...works well ( sed 's#.*/##; s/>$//;s/^[^_]*//; s/..*//' | rev | cut -d'' -f1 | rev ). output is : F00ME001 :)))) Thank you very much once again.
– Nayak
Jan 31 at 14:39
|
show 1 more comment
Tried with below commands to achieve same
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
o1=`echo $final_model| awk -F "/" '{print $NF}'|sed "s/[^[a-zA-Z_0-9.]//g"`
echo $o1
o2=`echo $o1|awk -F [_.] '{print $2}'`
echo $o2
F00ME001
I prefer to not fix from the start position: awk -F [_.] '{print $(NF-1)}'
– BrenoZan
Jan 29 at 18:31
You can not have run that first line. It contains a syntax error.
– Kusalananda
Jan 29 at 19:38
add a comment |
$ final_model="</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>"
$ o1=$(echo "$final_model" | sed -e 's/<(.*)>/1/') #remove <>
$ o1=$(basename "$o1") #get basename (test_F00ME001.inp)
$ echo "$o1"
test_F00ME001.inp
$ o2=$(echo "$o1" | sed -e 's/test_(.*).inp/1/') #get text between test_ and .inp
$ echo "$o2"
F00ME001
Try more use google next time, you can find both commands answered somewhere.
sed
command source: https://stackoverflow.com/a/13242517/7351855
Note that the$final_model
string in tho question is enclosed in<...>
, and that it's unclear whether that line shows a variable assignment or wether that line is just a piece of text coming out ofgrep
.
– Kusalananda
Jan 29 at 19:27
@Kusalananda Thanks for the point, I edited post to remove also <> fromfinal_model
string.
– Matej
Jan 29 at 20:20
add a comment |
replace the grep -i final_model
with perl, and you capture the data you need:
$ perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
test_F00ME001.inp
F00ME001
The first set of capturing parentheses grabs the stuff after the last slash: (test_([^.]+)[^>]+)
The second set grabs the stuff between the underscore and the dot.
Then you can capture the 2 lines into variables with this construct:
{ read output1; read output2; } < <(
perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
)
We are redirecting a Process Substitution into the grouping of 2 read commands.
$ echo "$output1"
test_F00ME001.inp
$ echo "$output2"
F00ME001
thanks for answer... i am avoiding additional script/language as Perl ( trying to execute with Shell).
– Nayak
Jan 30 at 17:12
1
Why? From the shell's perspective, perl and grep are both simply external commands: they are not shell commands. Are you looking for a pure shell solution? If yes, what shell and version are you using?
– glenn jackman
Jan 30 at 18:26
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming that
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
is a line of text that your grep
command is producing,
grep ... | sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
This would save the two modified lines into output1
and output2
Testing:
$ printf '%sn' 'final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>' |
sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
$ cat output1
test_F00ME001.inp
$ cat output2
F00ME001
The first sed
call operates on the original data and first removes everything up to and including the last /
character. It then removes the >
at the end.
The tee
saves this into output1
and also passes it on to the next stage of the pipeline.
The second sed
call operates on the modified data and first removes everything up to and including the first _
character. It then removes everything from the first dot onwards and redirects the result into output2
.
Thanks... output-1 works well, but Output-2 is ( model=</dir1/dir2/dir3/user/dir4/test_F00ME001 )
– Nayak
Jan 31 at 13:42
@Nayak That's because you are running sed separately on the grep output. This is not what I'm doing. I'm running the second sed on the result of the first.
– Kusalananda
Jan 31 at 13:50
i have added second command with pipe (on output of first sed). now result looks like (test_F00ME001). what if i have more than one "_" symbol ?!
– Nayak
Jan 31 at 14:20
@Nayak Well, that depends on where it's located.
– Kusalananda
Jan 31 at 14:34
now, i have combined both sed commands, then with pipe added rev and cut...works well ( sed 's#.*/##; s/>$//;s/^[^_]*//; s/..*//' | rev | cut -d'' -f1 | rev ). output is : F00ME001 :)))) Thank you very much once again.
– Nayak
Jan 31 at 14:39
|
show 1 more comment
Assuming that
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
is a line of text that your grep
command is producing,
grep ... | sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
This would save the two modified lines into output1
and output2
Testing:
$ printf '%sn' 'final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>' |
sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
$ cat output1
test_F00ME001.inp
$ cat output2
F00ME001
The first sed
call operates on the original data and first removes everything up to and including the last /
character. It then removes the >
at the end.
The tee
saves this into output1
and also passes it on to the next stage of the pipeline.
The second sed
call operates on the modified data and first removes everything up to and including the first _
character. It then removes everything from the first dot onwards and redirects the result into output2
.
Thanks... output-1 works well, but Output-2 is ( model=</dir1/dir2/dir3/user/dir4/test_F00ME001 )
– Nayak
Jan 31 at 13:42
@Nayak That's because you are running sed separately on the grep output. This is not what I'm doing. I'm running the second sed on the result of the first.
– Kusalananda
Jan 31 at 13:50
i have added second command with pipe (on output of first sed). now result looks like (test_F00ME001). what if i have more than one "_" symbol ?!
– Nayak
Jan 31 at 14:20
@Nayak Well, that depends on where it's located.
– Kusalananda
Jan 31 at 14:34
now, i have combined both sed commands, then with pipe added rev and cut...works well ( sed 's#.*/##; s/>$//;s/^[^_]*//; s/..*//' | rev | cut -d'' -f1 | rev ). output is : F00ME001 :)))) Thank you very much once again.
– Nayak
Jan 31 at 14:39
|
show 1 more comment
Assuming that
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
is a line of text that your grep
command is producing,
grep ... | sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
This would save the two modified lines into output1
and output2
Testing:
$ printf '%sn' 'final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>' |
sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
$ cat output1
test_F00ME001.inp
$ cat output2
F00ME001
The first sed
call operates on the original data and first removes everything up to and including the last /
character. It then removes the >
at the end.
The tee
saves this into output1
and also passes it on to the next stage of the pipeline.
The second sed
call operates on the modified data and first removes everything up to and including the first _
character. It then removes everything from the first dot onwards and redirects the result into output2
.
Assuming that
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
is a line of text that your grep
command is producing,
grep ... | sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
This would save the two modified lines into output1
and output2
Testing:
$ printf '%sn' 'final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>' |
sed 's#.*/##; s/>$//' | tee output1 |
sed 's/^[^_]*_//; s/..*//' >output2
$ cat output1
test_F00ME001.inp
$ cat output2
F00ME001
The first sed
call operates on the original data and first removes everything up to and including the last /
character. It then removes the >
at the end.
The tee
saves this into output1
and also passes it on to the next stage of the pipeline.
The second sed
call operates on the modified data and first removes everything up to and including the first _
character. It then removes everything from the first dot onwards and redirects the result into output2
.
answered Jan 29 at 19:36
KusalanandaKusalananda
129k16242399
129k16242399
Thanks... output-1 works well, but Output-2 is ( model=</dir1/dir2/dir3/user/dir4/test_F00ME001 )
– Nayak
Jan 31 at 13:42
@Nayak That's because you are running sed separately on the grep output. This is not what I'm doing. I'm running the second sed on the result of the first.
– Kusalananda
Jan 31 at 13:50
i have added second command with pipe (on output of first sed). now result looks like (test_F00ME001). what if i have more than one "_" symbol ?!
– Nayak
Jan 31 at 14:20
@Nayak Well, that depends on where it's located.
– Kusalananda
Jan 31 at 14:34
now, i have combined both sed commands, then with pipe added rev and cut...works well ( sed 's#.*/##; s/>$//;s/^[^_]*//; s/..*//' | rev | cut -d'' -f1 | rev ). output is : F00ME001 :)))) Thank you very much once again.
– Nayak
Jan 31 at 14:39
|
show 1 more comment
Thanks... output-1 works well, but Output-2 is ( model=</dir1/dir2/dir3/user/dir4/test_F00ME001 )
– Nayak
Jan 31 at 13:42
@Nayak That's because you are running sed separately on the grep output. This is not what I'm doing. I'm running the second sed on the result of the first.
– Kusalananda
Jan 31 at 13:50
i have added second command with pipe (on output of first sed). now result looks like (test_F00ME001). what if i have more than one "_" symbol ?!
– Nayak
Jan 31 at 14:20
@Nayak Well, that depends on where it's located.
– Kusalananda
Jan 31 at 14:34
now, i have combined both sed commands, then with pipe added rev and cut...works well ( sed 's#.*/##; s/>$//;s/^[^_]*//; s/..*//' | rev | cut -d'' -f1 | rev ). output is : F00ME001 :)))) Thank you very much once again.
– Nayak
Jan 31 at 14:39
Thanks... output-1 works well, but Output-2 is ( model=</dir1/dir2/dir3/user/dir4/test_F00ME001 )
– Nayak
Jan 31 at 13:42
Thanks... output-1 works well, but Output-2 is ( model=</dir1/dir2/dir3/user/dir4/test_F00ME001 )
– Nayak
Jan 31 at 13:42
@Nayak That's because you are running sed separately on the grep output. This is not what I'm doing. I'm running the second sed on the result of the first.
– Kusalananda
Jan 31 at 13:50
@Nayak That's because you are running sed separately on the grep output. This is not what I'm doing. I'm running the second sed on the result of the first.
– Kusalananda
Jan 31 at 13:50
i have added second command with pipe (on output of first sed). now result looks like (test_F00ME001). what if i have more than one "_" symbol ?!
– Nayak
Jan 31 at 14:20
i have added second command with pipe (on output of first sed). now result looks like (test_F00ME001). what if i have more than one "_" symbol ?!
– Nayak
Jan 31 at 14:20
@Nayak Well, that depends on where it's located.
– Kusalananda
Jan 31 at 14:34
@Nayak Well, that depends on where it's located.
– Kusalananda
Jan 31 at 14:34
now, i have combined both sed commands, then with pipe added rev and cut...works well ( sed 's#.*/##; s/>$//;s/^[^_]*//; s/..*//' | rev | cut -d'' -f1 | rev ). output is : F00ME001 :)))) Thank you very much once again.
– Nayak
Jan 31 at 14:39
now, i have combined both sed commands, then with pipe added rev and cut...works well ( sed 's#.*/##; s/>$//;s/^[^_]*//; s/..*//' | rev | cut -d'' -f1 | rev ). output is : F00ME001 :)))) Thank you very much once again.
– Nayak
Jan 31 at 14:39
|
show 1 more comment
Tried with below commands to achieve same
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
o1=`echo $final_model| awk -F "/" '{print $NF}'|sed "s/[^[a-zA-Z_0-9.]//g"`
echo $o1
o2=`echo $o1|awk -F [_.] '{print $2}'`
echo $o2
F00ME001
I prefer to not fix from the start position: awk -F [_.] '{print $(NF-1)}'
– BrenoZan
Jan 29 at 18:31
You can not have run that first line. It contains a syntax error.
– Kusalananda
Jan 29 at 19:38
add a comment |
Tried with below commands to achieve same
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
o1=`echo $final_model| awk -F "/" '{print $NF}'|sed "s/[^[a-zA-Z_0-9.]//g"`
echo $o1
o2=`echo $o1|awk -F [_.] '{print $2}'`
echo $o2
F00ME001
I prefer to not fix from the start position: awk -F [_.] '{print $(NF-1)}'
– BrenoZan
Jan 29 at 18:31
You can not have run that first line. It contains a syntax error.
– Kusalananda
Jan 29 at 19:38
add a comment |
Tried with below commands to achieve same
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
o1=`echo $final_model| awk -F "/" '{print $NF}'|sed "s/[^[a-zA-Z_0-9.]//g"`
echo $o1
o2=`echo $o1|awk -F [_.] '{print $2}'`
echo $o2
F00ME001
Tried with below commands to achieve same
final_model=</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>
o1=`echo $final_model| awk -F "/" '{print $NF}'|sed "s/[^[a-zA-Z_0-9.]//g"`
echo $o1
o2=`echo $o1|awk -F [_.] '{print $2}'`
echo $o2
F00ME001
answered Jan 29 at 16:22
Praveen Kumar BSPraveen Kumar BS
1,464138
1,464138
I prefer to not fix from the start position: awk -F [_.] '{print $(NF-1)}'
– BrenoZan
Jan 29 at 18:31
You can not have run that first line. It contains a syntax error.
– Kusalananda
Jan 29 at 19:38
add a comment |
I prefer to not fix from the start position: awk -F [_.] '{print $(NF-1)}'
– BrenoZan
Jan 29 at 18:31
You can not have run that first line. It contains a syntax error.
– Kusalananda
Jan 29 at 19:38
I prefer to not fix from the start position: awk -F [_.] '{print $(NF-1)}'
– BrenoZan
Jan 29 at 18:31
I prefer to not fix from the start position: awk -F [_.] '{print $(NF-1)}'
– BrenoZan
Jan 29 at 18:31
You can not have run that first line. It contains a syntax error.
– Kusalananda
Jan 29 at 19:38
You can not have run that first line. It contains a syntax error.
– Kusalananda
Jan 29 at 19:38
add a comment |
$ final_model="</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>"
$ o1=$(echo "$final_model" | sed -e 's/<(.*)>/1/') #remove <>
$ o1=$(basename "$o1") #get basename (test_F00ME001.inp)
$ echo "$o1"
test_F00ME001.inp
$ o2=$(echo "$o1" | sed -e 's/test_(.*).inp/1/') #get text between test_ and .inp
$ echo "$o2"
F00ME001
Try more use google next time, you can find both commands answered somewhere.
sed
command source: https://stackoverflow.com/a/13242517/7351855
Note that the$final_model
string in tho question is enclosed in<...>
, and that it's unclear whether that line shows a variable assignment or wether that line is just a piece of text coming out ofgrep
.
– Kusalananda
Jan 29 at 19:27
@Kusalananda Thanks for the point, I edited post to remove also <> fromfinal_model
string.
– Matej
Jan 29 at 20:20
add a comment |
$ final_model="</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>"
$ o1=$(echo "$final_model" | sed -e 's/<(.*)>/1/') #remove <>
$ o1=$(basename "$o1") #get basename (test_F00ME001.inp)
$ echo "$o1"
test_F00ME001.inp
$ o2=$(echo "$o1" | sed -e 's/test_(.*).inp/1/') #get text between test_ and .inp
$ echo "$o2"
F00ME001
Try more use google next time, you can find both commands answered somewhere.
sed
command source: https://stackoverflow.com/a/13242517/7351855
Note that the$final_model
string in tho question is enclosed in<...>
, and that it's unclear whether that line shows a variable assignment or wether that line is just a piece of text coming out ofgrep
.
– Kusalananda
Jan 29 at 19:27
@Kusalananda Thanks for the point, I edited post to remove also <> fromfinal_model
string.
– Matej
Jan 29 at 20:20
add a comment |
$ final_model="</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>"
$ o1=$(echo "$final_model" | sed -e 's/<(.*)>/1/') #remove <>
$ o1=$(basename "$o1") #get basename (test_F00ME001.inp)
$ echo "$o1"
test_F00ME001.inp
$ o2=$(echo "$o1" | sed -e 's/test_(.*).inp/1/') #get text between test_ and .inp
$ echo "$o2"
F00ME001
Try more use google next time, you can find both commands answered somewhere.
sed
command source: https://stackoverflow.com/a/13242517/7351855
$ final_model="</dir1/dir2/dir3/user/dir4/test_F00ME001.inp>"
$ o1=$(echo "$final_model" | sed -e 's/<(.*)>/1/') #remove <>
$ o1=$(basename "$o1") #get basename (test_F00ME001.inp)
$ echo "$o1"
test_F00ME001.inp
$ o2=$(echo "$o1" | sed -e 's/test_(.*).inp/1/') #get text between test_ and .inp
$ echo "$o2"
F00ME001
Try more use google next time, you can find both commands answered somewhere.
sed
command source: https://stackoverflow.com/a/13242517/7351855
edited Jan 29 at 20:19
answered Jan 29 at 13:45
MatejMatej
866
866
Note that the$final_model
string in tho question is enclosed in<...>
, and that it's unclear whether that line shows a variable assignment or wether that line is just a piece of text coming out ofgrep
.
– Kusalananda
Jan 29 at 19:27
@Kusalananda Thanks for the point, I edited post to remove also <> fromfinal_model
string.
– Matej
Jan 29 at 20:20
add a comment |
Note that the$final_model
string in tho question is enclosed in<...>
, and that it's unclear whether that line shows a variable assignment or wether that line is just a piece of text coming out ofgrep
.
– Kusalananda
Jan 29 at 19:27
@Kusalananda Thanks for the point, I edited post to remove also <> fromfinal_model
string.
– Matej
Jan 29 at 20:20
Note that the
$final_model
string in tho question is enclosed in <...>
, and that it's unclear whether that line shows a variable assignment or wether that line is just a piece of text coming out of grep
.– Kusalananda
Jan 29 at 19:27
Note that the
$final_model
string in tho question is enclosed in <...>
, and that it's unclear whether that line shows a variable assignment or wether that line is just a piece of text coming out of grep
.– Kusalananda
Jan 29 at 19:27
@Kusalananda Thanks for the point, I edited post to remove also <> from
final_model
string.– Matej
Jan 29 at 20:20
@Kusalananda Thanks for the point, I edited post to remove also <> from
final_model
string.– Matej
Jan 29 at 20:20
add a comment |
replace the grep -i final_model
with perl, and you capture the data you need:
$ perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
test_F00ME001.inp
F00ME001
The first set of capturing parentheses grabs the stuff after the last slash: (test_([^.]+)[^>]+)
The second set grabs the stuff between the underscore and the dot.
Then you can capture the 2 lines into variables with this construct:
{ read output1; read output2; } < <(
perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
)
We are redirecting a Process Substitution into the grouping of 2 read commands.
$ echo "$output1"
test_F00ME001.inp
$ echo "$output2"
F00ME001
thanks for answer... i am avoiding additional script/language as Perl ( trying to execute with Shell).
– Nayak
Jan 30 at 17:12
1
Why? From the shell's perspective, perl and grep are both simply external commands: they are not shell commands. Are you looking for a pure shell solution? If yes, what shell and version are you using?
– glenn jackman
Jan 30 at 18:26
add a comment |
replace the grep -i final_model
with perl, and you capture the data you need:
$ perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
test_F00ME001.inp
F00ME001
The first set of capturing parentheses grabs the stuff after the last slash: (test_([^.]+)[^>]+)
The second set grabs the stuff between the underscore and the dot.
Then you can capture the 2 lines into variables with this construct:
{ read output1; read output2; } < <(
perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
)
We are redirecting a Process Substitution into the grouping of 2 read commands.
$ echo "$output1"
test_F00ME001.inp
$ echo "$output2"
F00ME001
thanks for answer... i am avoiding additional script/language as Perl ( trying to execute with Shell).
– Nayak
Jan 30 at 17:12
1
Why? From the shell's perspective, perl and grep are both simply external commands: they are not shell commands. Are you looking for a pure shell solution? If yes, what shell and version are you using?
– glenn jackman
Jan 30 at 18:26
add a comment |
replace the grep -i final_model
with perl, and you capture the data you need:
$ perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
test_F00ME001.inp
F00ME001
The first set of capturing parentheses grabs the stuff after the last slash: (test_([^.]+)[^>]+)
The second set grabs the stuff between the underscore and the dot.
Then you can capture the 2 lines into variables with this construct:
{ read output1; read output2; } < <(
perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
)
We are redirecting a Process Substitution into the grouping of 2 read commands.
$ echo "$output1"
test_F00ME001.inp
$ echo "$output2"
F00ME001
replace the grep -i final_model
with perl, and you capture the data you need:
$ perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
test_F00ME001.inp
F00ME001
The first set of capturing parentheses grabs the stuff after the last slash: (test_([^.]+)[^>]+)
The second set grabs the stuff between the underscore and the dot.
Then you can capture the 2 lines into variables with this construct:
{ read output1; read output2; } < <(
perl -nE '/^final_model=.*/(test_([^.]+)[^>]+)/ && say "$1n$2"' assemble.preprocessing
)
We are redirecting a Process Substitution into the grouping of 2 read commands.
$ echo "$output1"
test_F00ME001.inp
$ echo "$output2"
F00ME001
answered Jan 29 at 18:21
glenn jackmanglenn jackman
51.4k571111
51.4k571111
thanks for answer... i am avoiding additional script/language as Perl ( trying to execute with Shell).
– Nayak
Jan 30 at 17:12
1
Why? From the shell's perspective, perl and grep are both simply external commands: they are not shell commands. Are you looking for a pure shell solution? If yes, what shell and version are you using?
– glenn jackman
Jan 30 at 18:26
add a comment |
thanks for answer... i am avoiding additional script/language as Perl ( trying to execute with Shell).
– Nayak
Jan 30 at 17:12
1
Why? From the shell's perspective, perl and grep are both simply external commands: they are not shell commands. Are you looking for a pure shell solution? If yes, what shell and version are you using?
– glenn jackman
Jan 30 at 18:26
thanks for answer... i am avoiding additional script/language as Perl ( trying to execute with Shell).
– Nayak
Jan 30 at 17:12
thanks for answer... i am avoiding additional script/language as Perl ( trying to execute with Shell).
– Nayak
Jan 30 at 17:12
1
1
Why? From the shell's perspective, perl and grep are both simply external commands: they are not shell commands. Are you looking for a pure shell solution? If yes, what shell and version are you using?
– glenn jackman
Jan 30 at 18:26
Why? From the shell's perspective, perl and grep are both simply external commands: they are not shell commands. Are you looking for a pure shell solution? If yes, what shell and version are you using?
– glenn jackman
Jan 30 at 18:26
add a comment |
6
Please invest more time in writing your question. How does your input look like? How does the command you've tried looks like exactly?
– finswimmer
Jan 29 at 13:10
And where does the output go? files? variables?
– Jeff Schaller
Jan 29 at 13:26
@Jeff Schaller : problem solved. Thanks for your contribution. please remove "HOLD" from my question !!
– Nayak
Jan 31 at 14:41
@Nayak, I am unable to take your question off-hold on my own; there's a review queue dedicated to reopening questions. I personally would not vote to reopen the question yet as my question (in a comment above) has not been answered. Happy to hear you got an answer, though!
– Jeff Schaller
Jan 31 at 14:50
output is displayed on shell
– Nayak
Jan 31 at 14:53