Parse delimited string using awk and fetch the matched string
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a delimited string variable as mentioned below. I would like to grep a matched string. I found some possible solutions on the Internet, but sadly they did not give me the result I was expecting. Can you suggest or correct me.
Input: 123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
Output: src_12
src_23
src_14
src_5
Logic: I need to fetch the string which has 'src_'. It's not always the second item in the list. The position may change. Variable length, delimited.
unix awk sed
add a comment |
I have a delimited string variable as mentioned below. I would like to grep a matched string. I found some possible solutions on the Internet, but sadly they did not give me the result I was expecting. Can you suggest or correct me.
Input: 123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
Output: src_12
src_23
src_14
src_5
Logic: I need to fetch the string which has 'src_'. It's not always the second item in the list. The position may change. Variable length, delimited.
unix awk sed
With GNU grep:grep -Po 'src_.*?b' file
?
– Cyrus
Jan 3 at 6:21
Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.
– srinath
Jan 3 at 6:23
1
grep -Po 'bsrc_.*?b' <<< "$variable"
?
– Cyrus
Jan 3 at 6:34
What part of this question involvesbash
shell and needs the tag?
– Inian
Jan 3 at 6:41
Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?
– kvantour
Jan 3 at 10:40
add a comment |
I have a delimited string variable as mentioned below. I would like to grep a matched string. I found some possible solutions on the Internet, but sadly they did not give me the result I was expecting. Can you suggest or correct me.
Input: 123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
Output: src_12
src_23
src_14
src_5
Logic: I need to fetch the string which has 'src_'. It's not always the second item in the list. The position may change. Variable length, delimited.
unix awk sed
I have a delimited string variable as mentioned below. I would like to grep a matched string. I found some possible solutions on the Internet, but sadly they did not give me the result I was expecting. Can you suggest or correct me.
Input: 123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
Output: src_12
src_23
src_14
src_5
Logic: I need to fetch the string which has 'src_'. It's not always the second item in the list. The position may change. Variable length, delimited.
unix awk sed
unix awk sed
edited Jan 3 at 10:33
kvantour
10.7k41732
10.7k41732
asked Jan 3 at 6:18
srinathsrinath
447
447
With GNU grep:grep -Po 'src_.*?b' file
?
– Cyrus
Jan 3 at 6:21
Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.
– srinath
Jan 3 at 6:23
1
grep -Po 'bsrc_.*?b' <<< "$variable"
?
– Cyrus
Jan 3 at 6:34
What part of this question involvesbash
shell and needs the tag?
– Inian
Jan 3 at 6:41
Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?
– kvantour
Jan 3 at 10:40
add a comment |
With GNU grep:grep -Po 'src_.*?b' file
?
– Cyrus
Jan 3 at 6:21
Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.
– srinath
Jan 3 at 6:23
1
grep -Po 'bsrc_.*?b' <<< "$variable"
?
– Cyrus
Jan 3 at 6:34
What part of this question involvesbash
shell and needs the tag?
– Inian
Jan 3 at 6:41
Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?
– kvantour
Jan 3 at 10:40
With GNU grep:
grep -Po 'src_.*?b' file
?– Cyrus
Jan 3 at 6:21
With GNU grep:
grep -Po 'src_.*?b' file
?– Cyrus
Jan 3 at 6:21
Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.
– srinath
Jan 3 at 6:23
Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.
– srinath
Jan 3 at 6:23
1
1
grep -Po 'bsrc_.*?b' <<< "$variable"
?– Cyrus
Jan 3 at 6:34
grep -Po 'bsrc_.*?b' <<< "$variable"
?– Cyrus
Jan 3 at 6:34
What part of this question involves
bash
shell and needs the tag?– Inian
Jan 3 at 6:41
What part of this question involves
bash
shell and needs the tag?– Inian
Jan 3 at 6:41
Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?
– kvantour
Jan 3 at 10:40
Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?
– kvantour
Jan 3 at 10:40
add a comment |
7 Answers
7
active
oldest
votes
With simple awk
solution:
awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file
or
awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file
1
When no match scenarios, it gives first string. How to avoid it
– srinath
Jan 4 at 2:14
@srinath, please try to run first command only which should give output when a match is found only.
– RavinderSingh13
Jan 4 at 2:30
first statement fails when i have src_abc (non numeric) one.
– srinath
Jan 4 at 9:11
@srinath, trysrc_[0-9a-zA-Z]+
on place ofsrc_[0-9]+/
once and let know?
– RavinderSingh13
Jan 4 at 9:13
it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.
– srinath
Jan 4 at 9:24
|
show 2 more comments
With bash:
while IFS="," read -a array; do
for element in "${array[@]}"; do
[[ $element =~ ^src_ ]] && echo "$element"
done
done <<< "$variable"
Output:
src_12
src_23
src_14
src_5
add a comment |
Using tr
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ A=$(cat srinath.txt2)
$ tr ',' 'n' <<< "$A" | grep ^src
src_12
src_23
src_14
src_5
src_6
src_7
You can avoid thecat
, but this will also pick up stuff likevsrc_blabla
– kvantour
Jan 3 at 10:35
I wanted to post about the same answer, only usinggrep "^src_"
avoidingvsrc_blabla
– Walter A
Jan 3 at 10:37
Thanks @kvantour for the review.. updated the answer
– stack0114106
Jan 3 at 11:04
@Walter.. thank you.. updated the answer
– stack0114106
Jan 3 at 11:04
add a comment |
Using Perl
$ cat srinath.txt
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
$ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
src_12
src_23
src_14
src_5
If you have more than one src_ in the same line, then use below
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
src_12
src_23
src_14
src_5
src_6
src_7
If it is in a variable, then
$ A=$(cat srinath.txt2)
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
or
$ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
$ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
src_12
src_23
src_14
src_5
src_6
src_7
i need in unix/DOS. its in middle of my program.
– srinath
Jan 3 at 6:26
yes.. you can call perl in middle of the bash program
– stack0114106
Jan 3 at 6:28
updated the answer for working with variables
– stack0114106
Jan 3 at 6:31
Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.
– srinath
Jan 3 at 6:34
add a comment |
This might work for you (GNU sed):
sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file
Surround all candidate strings by newlines and then using the sed commands P
and D
whittle down each line printing only the candidates with the prefix src_
.
add a comment |
A simple grep returning only (-o
) the matched words (-w
)
$ grep -wo 'src_[^,]*' file
src_12
src_23
src_14
src_5
add a comment |
Look for ^src_xxx,
, ,src_xxx,
and ,src_xxx$
, and only print the match without the ,
.
sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017214%2fparse-delimited-string-using-awk-and-fetch-the-matched-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
With simple awk
solution:
awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file
or
awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file
1
When no match scenarios, it gives first string. How to avoid it
– srinath
Jan 4 at 2:14
@srinath, please try to run first command only which should give output when a match is found only.
– RavinderSingh13
Jan 4 at 2:30
first statement fails when i have src_abc (non numeric) one.
– srinath
Jan 4 at 9:11
@srinath, trysrc_[0-9a-zA-Z]+
on place ofsrc_[0-9]+/
once and let know?
– RavinderSingh13
Jan 4 at 9:13
it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.
– srinath
Jan 4 at 9:24
|
show 2 more comments
With simple awk
solution:
awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file
or
awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file
1
When no match scenarios, it gives first string. How to avoid it
– srinath
Jan 4 at 2:14
@srinath, please try to run first command only which should give output when a match is found only.
– RavinderSingh13
Jan 4 at 2:30
first statement fails when i have src_abc (non numeric) one.
– srinath
Jan 4 at 9:11
@srinath, trysrc_[0-9a-zA-Z]+
on place ofsrc_[0-9]+/
once and let know?
– RavinderSingh13
Jan 4 at 9:13
it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.
– srinath
Jan 4 at 9:24
|
show 2 more comments
With simple awk
solution:
awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file
or
awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file
With simple awk
solution:
awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file
or
awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file
edited Jan 3 at 10:31
kvantour
10.7k41732
10.7k41732
answered Jan 3 at 6:37
RavinderSingh13RavinderSingh13
30.6k41639
30.6k41639
1
When no match scenarios, it gives first string. How to avoid it
– srinath
Jan 4 at 2:14
@srinath, please try to run first command only which should give output when a match is found only.
– RavinderSingh13
Jan 4 at 2:30
first statement fails when i have src_abc (non numeric) one.
– srinath
Jan 4 at 9:11
@srinath, trysrc_[0-9a-zA-Z]+
on place ofsrc_[0-9]+/
once and let know?
– RavinderSingh13
Jan 4 at 9:13
it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.
– srinath
Jan 4 at 9:24
|
show 2 more comments
1
When no match scenarios, it gives first string. How to avoid it
– srinath
Jan 4 at 2:14
@srinath, please try to run first command only which should give output when a match is found only.
– RavinderSingh13
Jan 4 at 2:30
first statement fails when i have src_abc (non numeric) one.
– srinath
Jan 4 at 9:11
@srinath, trysrc_[0-9a-zA-Z]+
on place ofsrc_[0-9]+/
once and let know?
– RavinderSingh13
Jan 4 at 9:13
it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.
– srinath
Jan 4 at 9:24
1
1
When no match scenarios, it gives first string. How to avoid it
– srinath
Jan 4 at 2:14
When no match scenarios, it gives first string. How to avoid it
– srinath
Jan 4 at 2:14
@srinath, please try to run first command only which should give output when a match is found only.
– RavinderSingh13
Jan 4 at 2:30
@srinath, please try to run first command only which should give output when a match is found only.
– RavinderSingh13
Jan 4 at 2:30
first statement fails when i have src_abc (non numeric) one.
– srinath
Jan 4 at 9:11
first statement fails when i have src_abc (non numeric) one.
– srinath
Jan 4 at 9:11
@srinath, try
src_[0-9a-zA-Z]+
on place of src_[0-9]+/
once and let know?– RavinderSingh13
Jan 4 at 9:13
@srinath, try
src_[0-9a-zA-Z]+
on place of src_[0-9]+/
once and let know?– RavinderSingh13
Jan 4 at 9:13
it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.
– srinath
Jan 4 at 9:24
it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.
– srinath
Jan 4 at 9:24
|
show 2 more comments
With bash:
while IFS="," read -a array; do
for element in "${array[@]}"; do
[[ $element =~ ^src_ ]] && echo "$element"
done
done <<< "$variable"
Output:
src_12
src_23
src_14
src_5
add a comment |
With bash:
while IFS="," read -a array; do
for element in "${array[@]}"; do
[[ $element =~ ^src_ ]] && echo "$element"
done
done <<< "$variable"
Output:
src_12
src_23
src_14
src_5
add a comment |
With bash:
while IFS="," read -a array; do
for element in "${array[@]}"; do
[[ $element =~ ^src_ ]] && echo "$element"
done
done <<< "$variable"
Output:
src_12
src_23
src_14
src_5
With bash:
while IFS="," read -a array; do
for element in "${array[@]}"; do
[[ $element =~ ^src_ ]] && echo "$element"
done
done <<< "$variable"
Output:
src_12
src_23
src_14
src_5
answered Jan 3 at 6:38


CyrusCyrus
47.1k43880
47.1k43880
add a comment |
add a comment |
Using tr
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ A=$(cat srinath.txt2)
$ tr ',' 'n' <<< "$A" | grep ^src
src_12
src_23
src_14
src_5
src_6
src_7
You can avoid thecat
, but this will also pick up stuff likevsrc_blabla
– kvantour
Jan 3 at 10:35
I wanted to post about the same answer, only usinggrep "^src_"
avoidingvsrc_blabla
– Walter A
Jan 3 at 10:37
Thanks @kvantour for the review.. updated the answer
– stack0114106
Jan 3 at 11:04
@Walter.. thank you.. updated the answer
– stack0114106
Jan 3 at 11:04
add a comment |
Using tr
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ A=$(cat srinath.txt2)
$ tr ',' 'n' <<< "$A" | grep ^src
src_12
src_23
src_14
src_5
src_6
src_7
You can avoid thecat
, but this will also pick up stuff likevsrc_blabla
– kvantour
Jan 3 at 10:35
I wanted to post about the same answer, only usinggrep "^src_"
avoidingvsrc_blabla
– Walter A
Jan 3 at 10:37
Thanks @kvantour for the review.. updated the answer
– stack0114106
Jan 3 at 11:04
@Walter.. thank you.. updated the answer
– stack0114106
Jan 3 at 11:04
add a comment |
Using tr
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ A=$(cat srinath.txt2)
$ tr ',' 'n' <<< "$A" | grep ^src
src_12
src_23
src_14
src_5
src_6
src_7
Using tr
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ A=$(cat srinath.txt2)
$ tr ',' 'n' <<< "$A" | grep ^src
src_12
src_23
src_14
src_5
src_6
src_7
edited Jan 3 at 11:03
answered Jan 3 at 6:39
stack0114106stack0114106
4,9832423
4,9832423
You can avoid thecat
, but this will also pick up stuff likevsrc_blabla
– kvantour
Jan 3 at 10:35
I wanted to post about the same answer, only usinggrep "^src_"
avoidingvsrc_blabla
– Walter A
Jan 3 at 10:37
Thanks @kvantour for the review.. updated the answer
– stack0114106
Jan 3 at 11:04
@Walter.. thank you.. updated the answer
– stack0114106
Jan 3 at 11:04
add a comment |
You can avoid thecat
, but this will also pick up stuff likevsrc_blabla
– kvantour
Jan 3 at 10:35
I wanted to post about the same answer, only usinggrep "^src_"
avoidingvsrc_blabla
– Walter A
Jan 3 at 10:37
Thanks @kvantour for the review.. updated the answer
– stack0114106
Jan 3 at 11:04
@Walter.. thank you.. updated the answer
– stack0114106
Jan 3 at 11:04
You can avoid the
cat
, but this will also pick up stuff like vsrc_blabla
– kvantour
Jan 3 at 10:35
You can avoid the
cat
, but this will also pick up stuff like vsrc_blabla
– kvantour
Jan 3 at 10:35
I wanted to post about the same answer, only using
grep "^src_"
avoiding vsrc_blabla
– Walter A
Jan 3 at 10:37
I wanted to post about the same answer, only using
grep "^src_"
avoiding vsrc_blabla
– Walter A
Jan 3 at 10:37
Thanks @kvantour for the review.. updated the answer
– stack0114106
Jan 3 at 11:04
Thanks @kvantour for the review.. updated the answer
– stack0114106
Jan 3 at 11:04
@Walter.. thank you.. updated the answer
– stack0114106
Jan 3 at 11:04
@Walter.. thank you.. updated the answer
– stack0114106
Jan 3 at 11:04
add a comment |
Using Perl
$ cat srinath.txt
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
$ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
src_12
src_23
src_14
src_5
If you have more than one src_ in the same line, then use below
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
src_12
src_23
src_14
src_5
src_6
src_7
If it is in a variable, then
$ A=$(cat srinath.txt2)
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
or
$ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
$ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
src_12
src_23
src_14
src_5
src_6
src_7
i need in unix/DOS. its in middle of my program.
– srinath
Jan 3 at 6:26
yes.. you can call perl in middle of the bash program
– stack0114106
Jan 3 at 6:28
updated the answer for working with variables
– stack0114106
Jan 3 at 6:31
Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.
– srinath
Jan 3 at 6:34
add a comment |
Using Perl
$ cat srinath.txt
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
$ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
src_12
src_23
src_14
src_5
If you have more than one src_ in the same line, then use below
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
src_12
src_23
src_14
src_5
src_6
src_7
If it is in a variable, then
$ A=$(cat srinath.txt2)
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
or
$ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
$ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
src_12
src_23
src_14
src_5
src_6
src_7
i need in unix/DOS. its in middle of my program.
– srinath
Jan 3 at 6:26
yes.. you can call perl in middle of the bash program
– stack0114106
Jan 3 at 6:28
updated the answer for working with variables
– stack0114106
Jan 3 at 6:31
Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.
– srinath
Jan 3 at 6:34
add a comment |
Using Perl
$ cat srinath.txt
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
$ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
src_12
src_23
src_14
src_5
If you have more than one src_ in the same line, then use below
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
src_12
src_23
src_14
src_5
src_6
src_7
If it is in a variable, then
$ A=$(cat srinath.txt2)
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
or
$ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
$ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
src_12
src_23
src_14
src_5
src_6
src_7
Using Perl
$ cat srinath.txt
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
$ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
src_12
src_23
src_14
src_5
If you have more than one src_ in the same line, then use below
$ cat srinath.txt2
123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
src_6,src_7,16,18
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
src_12
src_23
src_14
src_5
src_6
src_7
If it is in a variable, then
$ A=$(cat srinath.txt2)
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
or
$ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"
$ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
src_12
src_23
src_14
src_5
src_6
src_7
$ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
src_12
src_23
src_14
src_5
src_6
src_7
edited Jan 3 at 6:34
answered Jan 3 at 6:25
stack0114106stack0114106
4,9832423
4,9832423
i need in unix/DOS. its in middle of my program.
– srinath
Jan 3 at 6:26
yes.. you can call perl in middle of the bash program
– stack0114106
Jan 3 at 6:28
updated the answer for working with variables
– stack0114106
Jan 3 at 6:31
Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.
– srinath
Jan 3 at 6:34
add a comment |
i need in unix/DOS. its in middle of my program.
– srinath
Jan 3 at 6:26
yes.. you can call perl in middle of the bash program
– stack0114106
Jan 3 at 6:28
updated the answer for working with variables
– stack0114106
Jan 3 at 6:31
Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.
– srinath
Jan 3 at 6:34
i need in unix/DOS. its in middle of my program.
– srinath
Jan 3 at 6:26
i need in unix/DOS. its in middle of my program.
– srinath
Jan 3 at 6:26
yes.. you can call perl in middle of the bash program
– stack0114106
Jan 3 at 6:28
yes.. you can call perl in middle of the bash program
– stack0114106
Jan 3 at 6:28
updated the answer for working with variables
– stack0114106
Jan 3 at 6:31
updated the answer for working with variables
– stack0114106
Jan 3 at 6:31
Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.
– srinath
Jan 3 at 6:34
Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.
– srinath
Jan 3 at 6:34
add a comment |
This might work for you (GNU sed):
sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file
Surround all candidate strings by newlines and then using the sed commands P
and D
whittle down each line printing only the candidates with the prefix src_
.
add a comment |
This might work for you (GNU sed):
sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file
Surround all candidate strings by newlines and then using the sed commands P
and D
whittle down each line printing only the candidates with the prefix src_
.
add a comment |
This might work for you (GNU sed):
sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file
Surround all candidate strings by newlines and then using the sed commands P
and D
whittle down each line printing only the candidates with the prefix src_
.
This might work for you (GNU sed):
sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file
Surround all candidate strings by newlines and then using the sed commands P
and D
whittle down each line printing only the candidates with the prefix src_
.
answered Jan 3 at 10:23
potongpotong
36.3k43062
36.3k43062
add a comment |
add a comment |
A simple grep returning only (-o
) the matched words (-w
)
$ grep -wo 'src_[^,]*' file
src_12
src_23
src_14
src_5
add a comment |
A simple grep returning only (-o
) the matched words (-w
)
$ grep -wo 'src_[^,]*' file
src_12
src_23
src_14
src_5
add a comment |
A simple grep returning only (-o
) the matched words (-w
)
$ grep -wo 'src_[^,]*' file
src_12
src_23
src_14
src_5
A simple grep returning only (-o
) the matched words (-w
)
$ grep -wo 'src_[^,]*' file
src_12
src_23
src_14
src_5
answered Jan 3 at 10:39
kvantourkvantour
10.7k41732
10.7k41732
add a comment |
add a comment |
Look for ^src_xxx,
, ,src_xxx,
and ,src_xxx$
, and only print the match without the ,
.
sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'
add a comment |
Look for ^src_xxx,
, ,src_xxx,
and ,src_xxx$
, and only print the match without the ,
.
sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'
add a comment |
Look for ^src_xxx,
, ,src_xxx,
and ,src_xxx$
, and only print the match without the ,
.
sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'
Look for ^src_xxx,
, ,src_xxx,
and ,src_xxx$
, and only print the match without the ,
.
sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'
answered Jan 3 at 10:49
Walter AWalter A
11.1k21132
11.1k21132
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017214%2fparse-delimited-string-using-awk-and-fetch-the-matched-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
With GNU grep:
grep -Po 'src_.*?b' file
?– Cyrus
Jan 3 at 6:21
Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.
– srinath
Jan 3 at 6:23
1
grep -Po 'bsrc_.*?b' <<< "$variable"
?– Cyrus
Jan 3 at 6:34
What part of this question involves
bash
shell and needs the tag?– Inian
Jan 3 at 6:41
Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?
– kvantour
Jan 3 at 10:40