How to get multiple set of strings from a file using specific patterns?
Edited
This is the content of example.dat file:
AAA*00* *00* *66*1234567890 *ZZ*ASD-LKJ *170630*1933*U*00401*100000023*0*P*~SS*IN*2035735845*JWN-DSD*20660606*0606*1023*X*DF4WE43R4RA~BIG*20660606*2771417*19990919*88146417~GE*3*1023~IEA*1*100000023~
The data I needed are inside a segment. A segment is separated by ~ and the data I needed is called an element. An element is separated by a *.
These are the pattern to find those strings:
Segments Element number Needed Data
AAA 6th, 8th & 13th 1234567890, ASD-LKJ & 100000023
SS 6th 1023
BIG 4th 88146417
GE 1st 3
Is there a command that outputs these data?
100000023 1023 3 1234567890 ASD-LKJ 88146417
Note: In the real world, the file contains really long set of strings and many segments but the pattern of segments and the place where the elements positioned stays the same.
linux
add a comment |
Edited
This is the content of example.dat file:
AAA*00* *00* *66*1234567890 *ZZ*ASD-LKJ *170630*1933*U*00401*100000023*0*P*~SS*IN*2035735845*JWN-DSD*20660606*0606*1023*X*DF4WE43R4RA~BIG*20660606*2771417*19990919*88146417~GE*3*1023~IEA*1*100000023~
The data I needed are inside a segment. A segment is separated by ~ and the data I needed is called an element. An element is separated by a *.
These are the pattern to find those strings:
Segments Element number Needed Data
AAA 6th, 8th & 13th 1234567890, ASD-LKJ & 100000023
SS 6th 1023
BIG 4th 88146417
GE 1st 3
Is there a command that outputs these data?
100000023 1023 3 1234567890 ASD-LKJ 88146417
Note: In the real world, the file contains really long set of strings and many segments but the pattern of segments and the place where the elements positioned stays the same.
linux
is the input single line or multiple line? what is the logic to extract strings? not able to see a pattern... and asAAAis only once in input string, how do you expect multiple output?
– Sundeep
Jul 4 '17 at 4:53
It's single line. Actually it's a file with a really long set of strings. I just shorten it for just an example. I'll update the patterns for everyone to understand further.
– WashichawbachaW
Jul 4 '17 at 7:24
1
it is still not clear on what basis you need to extract data from each of these segments... and why does last segment you show has~in it
– Sundeep
Jul 4 '17 at 7:43
Please edit the whole question again and give sample input and output.
– hschou
Jul 4 '17 at 9:23
Please see my last update. I think it's understandable enough. You can edit my question if you want.
– WashichawbachaW
Jul 5 '17 at 0:20
add a comment |
Edited
This is the content of example.dat file:
AAA*00* *00* *66*1234567890 *ZZ*ASD-LKJ *170630*1933*U*00401*100000023*0*P*~SS*IN*2035735845*JWN-DSD*20660606*0606*1023*X*DF4WE43R4RA~BIG*20660606*2771417*19990919*88146417~GE*3*1023~IEA*1*100000023~
The data I needed are inside a segment. A segment is separated by ~ and the data I needed is called an element. An element is separated by a *.
These are the pattern to find those strings:
Segments Element number Needed Data
AAA 6th, 8th & 13th 1234567890, ASD-LKJ & 100000023
SS 6th 1023
BIG 4th 88146417
GE 1st 3
Is there a command that outputs these data?
100000023 1023 3 1234567890 ASD-LKJ 88146417
Note: In the real world, the file contains really long set of strings and many segments but the pattern of segments and the place where the elements positioned stays the same.
linux
Edited
This is the content of example.dat file:
AAA*00* *00* *66*1234567890 *ZZ*ASD-LKJ *170630*1933*U*00401*100000023*0*P*~SS*IN*2035735845*JWN-DSD*20660606*0606*1023*X*DF4WE43R4RA~BIG*20660606*2771417*19990919*88146417~GE*3*1023~IEA*1*100000023~
The data I needed are inside a segment. A segment is separated by ~ and the data I needed is called an element. An element is separated by a *.
These are the pattern to find those strings:
Segments Element number Needed Data
AAA 6th, 8th & 13th 1234567890, ASD-LKJ & 100000023
SS 6th 1023
BIG 4th 88146417
GE 1st 3
Is there a command that outputs these data?
100000023 1023 3 1234567890 ASD-LKJ 88146417
Note: In the real world, the file contains really long set of strings and many segments but the pattern of segments and the place where the elements positioned stays the same.
linux
linux
edited Dec 15 at 22:00
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Jul 4 '17 at 4:15
WashichawbachaW
12711
12711
is the input single line or multiple line? what is the logic to extract strings? not able to see a pattern... and asAAAis only once in input string, how do you expect multiple output?
– Sundeep
Jul 4 '17 at 4:53
It's single line. Actually it's a file with a really long set of strings. I just shorten it for just an example. I'll update the patterns for everyone to understand further.
– WashichawbachaW
Jul 4 '17 at 7:24
1
it is still not clear on what basis you need to extract data from each of these segments... and why does last segment you show has~in it
– Sundeep
Jul 4 '17 at 7:43
Please edit the whole question again and give sample input and output.
– hschou
Jul 4 '17 at 9:23
Please see my last update. I think it's understandable enough. You can edit my question if you want.
– WashichawbachaW
Jul 5 '17 at 0:20
add a comment |
is the input single line or multiple line? what is the logic to extract strings? not able to see a pattern... and asAAAis only once in input string, how do you expect multiple output?
– Sundeep
Jul 4 '17 at 4:53
It's single line. Actually it's a file with a really long set of strings. I just shorten it for just an example. I'll update the patterns for everyone to understand further.
– WashichawbachaW
Jul 4 '17 at 7:24
1
it is still not clear on what basis you need to extract data from each of these segments... and why does last segment you show has~in it
– Sundeep
Jul 4 '17 at 7:43
Please edit the whole question again and give sample input and output.
– hschou
Jul 4 '17 at 9:23
Please see my last update. I think it's understandable enough. You can edit my question if you want.
– WashichawbachaW
Jul 5 '17 at 0:20
is the input single line or multiple line? what is the logic to extract strings? not able to see a pattern... and as
AAA is only once in input string, how do you expect multiple output?– Sundeep
Jul 4 '17 at 4:53
is the input single line or multiple line? what is the logic to extract strings? not able to see a pattern... and as
AAA is only once in input string, how do you expect multiple output?– Sundeep
Jul 4 '17 at 4:53
It's single line. Actually it's a file with a really long set of strings. I just shorten it for just an example. I'll update the patterns for everyone to understand further.
– WashichawbachaW
Jul 4 '17 at 7:24
It's single line. Actually it's a file with a really long set of strings. I just shorten it for just an example. I'll update the patterns for everyone to understand further.
– WashichawbachaW
Jul 4 '17 at 7:24
1
1
it is still not clear on what basis you need to extract data from each of these segments... and why does last segment you show has
~ in it– Sundeep
Jul 4 '17 at 7:43
it is still not clear on what basis you need to extract data from each of these segments... and why does last segment you show has
~ in it– Sundeep
Jul 4 '17 at 7:43
Please edit the whole question again and give sample input and output.
– hschou
Jul 4 '17 at 9:23
Please edit the whole question again and give sample input and output.
– hschou
Jul 4 '17 at 9:23
Please see my last update. I think it's understandable enough. You can edit my question if you want.
– WashichawbachaW
Jul 5 '17 at 0:20
Please see my last update. I think it's understandable enough. You can edit my question if you want.
– WashichawbachaW
Jul 5 '17 at 0:20
add a comment |
1 Answer
1
active
oldest
votes
This will output your example data in your example output format
$ cat script.awk
BEGIN { FS="*"; }
$1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }
$1=="SS" { print $7 }
$1=="BIG" { print $5 }
$1=="GE" { print $2 }
$ awk -f script.awk example.dat
1234567890
ASD-LKJ
100000023
1023
88146417
3
When I type "cat script.awk" it returns "cat: script.awk: No such file or directory". How to do that? Sorry I'm just new to Linux/Unix. Do you have a simpler command like 1 line just like grep?
– WashichawbachaW
Jul 5 '17 at 2:57
How to write that in shell? Do I do this: 1st to type:[current_directory]$ cat script.awk2nd:BEGIN { FS="*"; }[current_directory]$ 1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }[current_directory]$ 1=="SS" { print $7 }[current_directory]$ 1=="BIG" { print $5 }[current_directory]$ 1=="GE" { print $2 }[current_directory]$ awk -f script.awk example.datBut's it's not working for me. I'm doing it wrong. Kindly explain it further. I'm just new to Linux/Unix.
– WashichawbachaW
Jul 6 '17 at 8:47
Use a text editor such as vi or nano to create script.awk
– fpmurphy
Jul 7 '17 at 2:47
I triedvi script.awk, storedBEGIN { FS="*"; }up to$1=="GE" { print $2 }, save itchmod 755 script.awkand then finally run the commandawk -f script.awk example.datbut I only get these three output1234567890 ASD-LKJ 100000023
– WashichawbachaW
Jul 7 '17 at 4:06
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f375117%2fhow-to-get-multiple-set-of-strings-from-a-file-using-specific-patterns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This will output your example data in your example output format
$ cat script.awk
BEGIN { FS="*"; }
$1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }
$1=="SS" { print $7 }
$1=="BIG" { print $5 }
$1=="GE" { print $2 }
$ awk -f script.awk example.dat
1234567890
ASD-LKJ
100000023
1023
88146417
3
When I type "cat script.awk" it returns "cat: script.awk: No such file or directory". How to do that? Sorry I'm just new to Linux/Unix. Do you have a simpler command like 1 line just like grep?
– WashichawbachaW
Jul 5 '17 at 2:57
How to write that in shell? Do I do this: 1st to type:[current_directory]$ cat script.awk2nd:BEGIN { FS="*"; }[current_directory]$ 1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }[current_directory]$ 1=="SS" { print $7 }[current_directory]$ 1=="BIG" { print $5 }[current_directory]$ 1=="GE" { print $2 }[current_directory]$ awk -f script.awk example.datBut's it's not working for me. I'm doing it wrong. Kindly explain it further. I'm just new to Linux/Unix.
– WashichawbachaW
Jul 6 '17 at 8:47
Use a text editor such as vi or nano to create script.awk
– fpmurphy
Jul 7 '17 at 2:47
I triedvi script.awk, storedBEGIN { FS="*"; }up to$1=="GE" { print $2 }, save itchmod 755 script.awkand then finally run the commandawk -f script.awk example.datbut I only get these three output1234567890 ASD-LKJ 100000023
– WashichawbachaW
Jul 7 '17 at 4:06
add a comment |
This will output your example data in your example output format
$ cat script.awk
BEGIN { FS="*"; }
$1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }
$1=="SS" { print $7 }
$1=="BIG" { print $5 }
$1=="GE" { print $2 }
$ awk -f script.awk example.dat
1234567890
ASD-LKJ
100000023
1023
88146417
3
When I type "cat script.awk" it returns "cat: script.awk: No such file or directory". How to do that? Sorry I'm just new to Linux/Unix. Do you have a simpler command like 1 line just like grep?
– WashichawbachaW
Jul 5 '17 at 2:57
How to write that in shell? Do I do this: 1st to type:[current_directory]$ cat script.awk2nd:BEGIN { FS="*"; }[current_directory]$ 1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }[current_directory]$ 1=="SS" { print $7 }[current_directory]$ 1=="BIG" { print $5 }[current_directory]$ 1=="GE" { print $2 }[current_directory]$ awk -f script.awk example.datBut's it's not working for me. I'm doing it wrong. Kindly explain it further. I'm just new to Linux/Unix.
– WashichawbachaW
Jul 6 '17 at 8:47
Use a text editor such as vi or nano to create script.awk
– fpmurphy
Jul 7 '17 at 2:47
I triedvi script.awk, storedBEGIN { FS="*"; }up to$1=="GE" { print $2 }, save itchmod 755 script.awkand then finally run the commandawk -f script.awk example.datbut I only get these three output1234567890 ASD-LKJ 100000023
– WashichawbachaW
Jul 7 '17 at 4:06
add a comment |
This will output your example data in your example output format
$ cat script.awk
BEGIN { FS="*"; }
$1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }
$1=="SS" { print $7 }
$1=="BIG" { print $5 }
$1=="GE" { print $2 }
$ awk -f script.awk example.dat
1234567890
ASD-LKJ
100000023
1023
88146417
3
This will output your example data in your example output format
$ cat script.awk
BEGIN { FS="*"; }
$1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }
$1=="SS" { print $7 }
$1=="BIG" { print $5 }
$1=="GE" { print $2 }
$ awk -f script.awk example.dat
1234567890
ASD-LKJ
100000023
1023
88146417
3
answered Jul 5 '17 at 2:22
fpmurphy
2,353915
2,353915
When I type "cat script.awk" it returns "cat: script.awk: No such file or directory". How to do that? Sorry I'm just new to Linux/Unix. Do you have a simpler command like 1 line just like grep?
– WashichawbachaW
Jul 5 '17 at 2:57
How to write that in shell? Do I do this: 1st to type:[current_directory]$ cat script.awk2nd:BEGIN { FS="*"; }[current_directory]$ 1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }[current_directory]$ 1=="SS" { print $7 }[current_directory]$ 1=="BIG" { print $5 }[current_directory]$ 1=="GE" { print $2 }[current_directory]$ awk -f script.awk example.datBut's it's not working for me. I'm doing it wrong. Kindly explain it further. I'm just new to Linux/Unix.
– WashichawbachaW
Jul 6 '17 at 8:47
Use a text editor such as vi or nano to create script.awk
– fpmurphy
Jul 7 '17 at 2:47
I triedvi script.awk, storedBEGIN { FS="*"; }up to$1=="GE" { print $2 }, save itchmod 755 script.awkand then finally run the commandawk -f script.awk example.datbut I only get these three output1234567890 ASD-LKJ 100000023
– WashichawbachaW
Jul 7 '17 at 4:06
add a comment |
When I type "cat script.awk" it returns "cat: script.awk: No such file or directory". How to do that? Sorry I'm just new to Linux/Unix. Do you have a simpler command like 1 line just like grep?
– WashichawbachaW
Jul 5 '17 at 2:57
How to write that in shell? Do I do this: 1st to type:[current_directory]$ cat script.awk2nd:BEGIN { FS="*"; }[current_directory]$ 1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 }[current_directory]$ 1=="SS" { print $7 }[current_directory]$ 1=="BIG" { print $5 }[current_directory]$ 1=="GE" { print $2 }[current_directory]$ awk -f script.awk example.datBut's it's not working for me. I'm doing it wrong. Kindly explain it further. I'm just new to Linux/Unix.
– WashichawbachaW
Jul 6 '17 at 8:47
Use a text editor such as vi or nano to create script.awk
– fpmurphy
Jul 7 '17 at 2:47
I triedvi script.awk, storedBEGIN { FS="*"; }up to$1=="GE" { print $2 }, save itchmod 755 script.awkand then finally run the commandawk -f script.awk example.datbut I only get these three output1234567890 ASD-LKJ 100000023
– WashichawbachaW
Jul 7 '17 at 4:06
When I type "cat script.awk" it returns "cat: script.awk: No such file or directory". How to do that? Sorry I'm just new to Linux/Unix. Do you have a simpler command like 1 line just like grep?
– WashichawbachaW
Jul 5 '17 at 2:57
When I type "cat script.awk" it returns "cat: script.awk: No such file or directory". How to do that? Sorry I'm just new to Linux/Unix. Do you have a simpler command like 1 line just like grep?
– WashichawbachaW
Jul 5 '17 at 2:57
How to write that in shell? Do I do this: 1st to type:
[current_directory]$ cat script.awk 2nd: BEGIN { FS="*"; } [current_directory]$ 1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 } [current_directory]$ 1=="SS" { print $7 } [current_directory]$ 1=="BIG" { print $5 } [current_directory]$ 1=="GE" { print $2 } [current_directory]$ awk -f script.awk example.dat But's it's not working for me. I'm doing it wrong. Kindly explain it further. I'm just new to Linux/Unix.– WashichawbachaW
Jul 6 '17 at 8:47
How to write that in shell? Do I do this: 1st to type:
[current_directory]$ cat script.awk 2nd: BEGIN { FS="*"; } [current_directory]$ 1=="AAA" { printf "%sn%sn%sn", $7, $9, $14 } [current_directory]$ 1=="SS" { print $7 } [current_directory]$ 1=="BIG" { print $5 } [current_directory]$ 1=="GE" { print $2 } [current_directory]$ awk -f script.awk example.dat But's it's not working for me. I'm doing it wrong. Kindly explain it further. I'm just new to Linux/Unix.– WashichawbachaW
Jul 6 '17 at 8:47
Use a text editor such as vi or nano to create script.awk
– fpmurphy
Jul 7 '17 at 2:47
Use a text editor such as vi or nano to create script.awk
– fpmurphy
Jul 7 '17 at 2:47
I tried
vi script.awk, stored BEGIN { FS="*"; } up to $1=="GE" { print $2 }, save it chmod 755 script.awk and then finally run the command awk -f script.awk example.dat but I only get these three output 1234567890 ASD-LKJ 100000023– WashichawbachaW
Jul 7 '17 at 4:06
I tried
vi script.awk, stored BEGIN { FS="*"; } up to $1=="GE" { print $2 }, save it chmod 755 script.awk and then finally run the command awk -f script.awk example.dat but I only get these three output 1234567890 ASD-LKJ 100000023– WashichawbachaW
Jul 7 '17 at 4:06
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f375117%2fhow-to-get-multiple-set-of-strings-from-a-file-using-specific-patterns%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
is the input single line or multiple line? what is the logic to extract strings? not able to see a pattern... and as
AAAis only once in input string, how do you expect multiple output?– Sundeep
Jul 4 '17 at 4:53
It's single line. Actually it's a file with a really long set of strings. I just shorten it for just an example. I'll update the patterns for everyone to understand further.
– WashichawbachaW
Jul 4 '17 at 7:24
1
it is still not clear on what basis you need to extract data from each of these segments... and why does last segment you show has
~in it– Sundeep
Jul 4 '17 at 7:43
Please edit the whole question again and give sample input and output.
– hschou
Jul 4 '17 at 9:23
Please see my last update. I think it's understandable enough. You can edit my question if you want.
– WashichawbachaW
Jul 5 '17 at 0:20