How to use awk to extract data from a file based on the content of another file?How to use ^#$ as record separator in awk?awk : parse and write to another fileextract the data from 2 fileshow to change record or field separator of a fileHow to use Unindented line as the Record Separate in awk cliHow to EDIT only the last line (or any specific line number(s)) using awk command?Delete lines from one file if they contain a regex of content in another filemake awk print the line that match a variable and the next n lines and use a variable in awkUsing AWK To Extract Numbers From .CSV FileAwk: setting a record to a pattern match, then print only the last record
Do Rabbis admit emotional involvement in their rulings?
Why is there a cap on 401k contributions?
Why does the electron wavefunction not collapse within atoms at room temperature in gas, liquids or solids due to decoherence?
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
A Latin text with dependency tree
Is it a good idea to copy a trader when investing?
Has everyone forgotten about wildfire?
how to find out if there's files in a folder and exit accordingly (in KSH)
Why Faces eat each other?
Is it a Munchausen Number?
Can a planet still function with a damaged moon?
Row vectors and column vectors (Mathematica vs Matlab)
Lorentz invariance of Maxwell's equations in matter
Do Monks gain the 9th level Unarmored Movement benefit when wearing armor or using a shield?
How can I test a shell script in a "safe environment" to avoid harm to my computer?
Examples where existence is harder than evaluation
I might have messed up in the 'Future Work' section of my thesis
What is the Ancient One's mistake?
Names of the Six Tastes
Are wands in any sort of book going to be too much like Harry Potter?
resoldering copper waste pipe
How do carbureted and fuel injected engines compare in high altitude?
Are double contractions formal? Eg: "couldn't've" for "could not have"
What replaces x86 intrinsics for C when Apple ditches Intel CPUs for their own chips?
How to use awk to extract data from a file based on the content of another file?
How to use ^#$ as record separator in awk?awk : parse and write to another fileextract the data from 2 fileshow to change record or field separator of a fileHow to use Unindented line as the Record Separate in awk cliHow to EDIT only the last line (or any specific line number(s)) using awk command?Delete lines from one file if they contain a regex of content in another filemake awk print the line that match a variable and the next n lines and use a variable in awkUsing AWK To Extract Numbers From .CSV FileAwk: setting a record to a pattern match, then print only the last record
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have two files. One file includes structured data and be low is a sample.
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 2 title
article 2 body line 1
article 2 body line 2
article 2 body line 3
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
As you can see, +++ is the separator for records. For each record, the first line is the title, all other lines are the content of this record. Another file is a simple text file with a list of titles. For example:
article 1 title
article 3 title
article 4 title
What I want is the records with their title listed in the second file. So for the aforementioned example, the expected result is:
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
I think awk could probably solve my problem but I don't know how.
What I've tried is this:
awk 'BEGINRS="(r?n)?+3(r?n)?"; FS="r?n"; ORS="+++" NR==FNRa[$0];next ...' title_list.txt data.txt
My problem is that the RS for the two files should be different and I don't know how to make it work.
text-processing awk
add a comment |
I have two files. One file includes structured data and be low is a sample.
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 2 title
article 2 body line 1
article 2 body line 2
article 2 body line 3
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
As you can see, +++ is the separator for records. For each record, the first line is the title, all other lines are the content of this record. Another file is a simple text file with a list of titles. For example:
article 1 title
article 3 title
article 4 title
What I want is the records with their title listed in the second file. So for the aforementioned example, the expected result is:
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
I think awk could probably solve my problem but I don't know how.
What I've tried is this:
awk 'BEGINRS="(r?n)?+3(r?n)?"; FS="r?n"; ORS="+++" NR==FNRa[$0];next ...' title_list.txt data.txt
My problem is that the RS for the two files should be different and I don't know how to make it work.
text-processing awk
add a comment |
I have two files. One file includes structured data and be low is a sample.
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 2 title
article 2 body line 1
article 2 body line 2
article 2 body line 3
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
As you can see, +++ is the separator for records. For each record, the first line is the title, all other lines are the content of this record. Another file is a simple text file with a list of titles. For example:
article 1 title
article 3 title
article 4 title
What I want is the records with their title listed in the second file. So for the aforementioned example, the expected result is:
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
I think awk could probably solve my problem but I don't know how.
What I've tried is this:
awk 'BEGINRS="(r?n)?+3(r?n)?"; FS="r?n"; ORS="+++" NR==FNRa[$0];next ...' title_list.txt data.txt
My problem is that the RS for the two files should be different and I don't know how to make it work.
text-processing awk
I have two files. One file includes structured data and be low is a sample.
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 2 title
article 2 body line 1
article 2 body line 2
article 2 body line 3
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
As you can see, +++ is the separator for records. For each record, the first line is the title, all other lines are the content of this record. Another file is a simple text file with a list of titles. For example:
article 1 title
article 3 title
article 4 title
What I want is the records with their title listed in the second file. So for the aforementioned example, the expected result is:
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
I think awk could probably solve my problem but I don't know how.
What I've tried is this:
awk 'BEGINRS="(r?n)?+3(r?n)?"; FS="r?n"; ORS="+++" NR==FNRa[$0];next ...' title_list.txt data.txt
My problem is that the RS for the two files should be different and I don't know how to make it work.
text-processing awk
text-processing awk
asked May 5 at 20:00
Ogrish ManOgrish Man
6141616
6141616
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can set variables like RS separately for each file. For example:
$ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
+++
add a comment |
In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:
$ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
add a comment |
Done by below method using combination of sed and awk
command
k=`awk 'print NR' file2| sed -n '$p'`
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
output
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
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%2f517265%2fhow-to-use-awk-to-extract-data-from-a-file-based-on-the-content-of-another-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can set variables like RS separately for each file. For example:
$ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
+++
add a comment |
You can set variables like RS separately for each file. For example:
$ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
+++
add a comment |
You can set variables like RS separately for each file. For example:
$ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
+++
You can set variables like RS separately for each file. For example:
$ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
+++
answered May 5 at 21:26
John1024John1024
49.1k5114129
49.1k5114129
add a comment |
add a comment |
In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:
$ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
add a comment |
In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:
$ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
add a comment |
In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:
$ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:
$ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
answered May 5 at 21:36
jimmijjimmij
32.9k877112
32.9k877112
add a comment |
add a comment |
Done by below method using combination of sed and awk
command
k=`awk 'print NR' file2| sed -n '$p'`
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
output
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
add a comment |
Done by below method using combination of sed and awk
command
k=`awk 'print NR' file2| sed -n '$p'`
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
output
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
add a comment |
Done by below method using combination of sed and awk
command
k=`awk 'print NR' file2| sed -n '$p'`
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
output
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
Done by below method using combination of sed and awk
command
k=`awk 'print NR' file2| sed -n '$p'`
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
output
for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3
answered 2 days ago
Praveen Kumar BSPraveen Kumar BS
1,8532311
1,8532311
add a comment |
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.
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%2f517265%2fhow-to-use-awk-to-extract-data-from-a-file-based-on-the-content-of-another-file%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