How to print files names from file where awk is selecting values?












0















I have a .txt file having files names as



z1.cap
z2.cap
z3.cap
z4.cap


Sample data present in these files are like shown below,



OTR 25896 PAT210 $TREMD DEST

OFR 21475 NAT102 #TREMD DEST


then I'm using below code to print desired values from files.



while read file_name
do
echo "progressing with file :${file_name}"
cat ${file_name} | grep "PAT210" | awk -F' ' '$5 == "(DEST" { print $file_name, $1}' | uniq >> OUTPUT_FILE


Now I want output which consists of 2 fields like,



z1.cap OTR
z2.cap OFR


and so on...



But i'm getting ouputs like,



- OTR
- OFR


Any help is aprreciated, Thanks.










share|improve this question

























  • I am trying to understand what you want by reading the code that has problems. Can you explain why you find z2.cap OFR when the code looks for PAT210?

    – Walter A
    Nov 21 '18 at 21:58











  • Inside the awk code, shell variables are not replaced. For a filename input you can use FILENAME, in other cases use awk -v awkvariable=$shellvariable '...awkcode...'.

    – Walter A
    Nov 21 '18 at 22:01











  • @WalterA I am also at a loss as to what the OP is trying to achieve. It took some back and forth to get the question into its current state. Decided to leave well-enough alone.

    – Niall Cosgrove
    Nov 22 '18 at 20:30
















0















I have a .txt file having files names as



z1.cap
z2.cap
z3.cap
z4.cap


Sample data present in these files are like shown below,



OTR 25896 PAT210 $TREMD DEST

OFR 21475 NAT102 #TREMD DEST


then I'm using below code to print desired values from files.



while read file_name
do
echo "progressing with file :${file_name}"
cat ${file_name} | grep "PAT210" | awk -F' ' '$5 == "(DEST" { print $file_name, $1}' | uniq >> OUTPUT_FILE


Now I want output which consists of 2 fields like,



z1.cap OTR
z2.cap OFR


and so on...



But i'm getting ouputs like,



- OTR
- OFR


Any help is aprreciated, Thanks.










share|improve this question

























  • I am trying to understand what you want by reading the code that has problems. Can you explain why you find z2.cap OFR when the code looks for PAT210?

    – Walter A
    Nov 21 '18 at 21:58











  • Inside the awk code, shell variables are not replaced. For a filename input you can use FILENAME, in other cases use awk -v awkvariable=$shellvariable '...awkcode...'.

    – Walter A
    Nov 21 '18 at 22:01











  • @WalterA I am also at a loss as to what the OP is trying to achieve. It took some back and forth to get the question into its current state. Decided to leave well-enough alone.

    – Niall Cosgrove
    Nov 22 '18 at 20:30














0












0








0


1






I have a .txt file having files names as



z1.cap
z2.cap
z3.cap
z4.cap


Sample data present in these files are like shown below,



OTR 25896 PAT210 $TREMD DEST

OFR 21475 NAT102 #TREMD DEST


then I'm using below code to print desired values from files.



while read file_name
do
echo "progressing with file :${file_name}"
cat ${file_name} | grep "PAT210" | awk -F' ' '$5 == "(DEST" { print $file_name, $1}' | uniq >> OUTPUT_FILE


Now I want output which consists of 2 fields like,



z1.cap OTR
z2.cap OFR


and so on...



But i'm getting ouputs like,



- OTR
- OFR


Any help is aprreciated, Thanks.










share|improve this question
















I have a .txt file having files names as



z1.cap
z2.cap
z3.cap
z4.cap


Sample data present in these files are like shown below,



OTR 25896 PAT210 $TREMD DEST

OFR 21475 NAT102 #TREMD DEST


then I'm using below code to print desired values from files.



while read file_name
do
echo "progressing with file :${file_name}"
cat ${file_name} | grep "PAT210" | awk -F' ' '$5 == "(DEST" { print $file_name, $1}' | uniq >> OUTPUT_FILE


Now I want output which consists of 2 fields like,



z1.cap OTR
z2.cap OFR


and so on...



But i'm getting ouputs like,



- OTR
- OFR


Any help is aprreciated, Thanks.







linux shell unix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 12:43







karthik

















asked Nov 21 '18 at 11:35









karthikkarthik

64




64













  • I am trying to understand what you want by reading the code that has problems. Can you explain why you find z2.cap OFR when the code looks for PAT210?

    – Walter A
    Nov 21 '18 at 21:58











  • Inside the awk code, shell variables are not replaced. For a filename input you can use FILENAME, in other cases use awk -v awkvariable=$shellvariable '...awkcode...'.

    – Walter A
    Nov 21 '18 at 22:01











  • @WalterA I am also at a loss as to what the OP is trying to achieve. It took some back and forth to get the question into its current state. Decided to leave well-enough alone.

    – Niall Cosgrove
    Nov 22 '18 at 20:30



















  • I am trying to understand what you want by reading the code that has problems. Can you explain why you find z2.cap OFR when the code looks for PAT210?

    – Walter A
    Nov 21 '18 at 21:58











  • Inside the awk code, shell variables are not replaced. For a filename input you can use FILENAME, in other cases use awk -v awkvariable=$shellvariable '...awkcode...'.

    – Walter A
    Nov 21 '18 at 22:01











  • @WalterA I am also at a loss as to what the OP is trying to achieve. It took some back and forth to get the question into its current state. Decided to leave well-enough alone.

    – Niall Cosgrove
    Nov 22 '18 at 20:30

















I am trying to understand what you want by reading the code that has problems. Can you explain why you find z2.cap OFR when the code looks for PAT210?

– Walter A
Nov 21 '18 at 21:58





I am trying to understand what you want by reading the code that has problems. Can you explain why you find z2.cap OFR when the code looks for PAT210?

– Walter A
Nov 21 '18 at 21:58













Inside the awk code, shell variables are not replaced. For a filename input you can use FILENAME, in other cases use awk -v awkvariable=$shellvariable '...awkcode...'.

– Walter A
Nov 21 '18 at 22:01





Inside the awk code, shell variables are not replaced. For a filename input you can use FILENAME, in other cases use awk -v awkvariable=$shellvariable '...awkcode...'.

– Walter A
Nov 21 '18 at 22:01













@WalterA I am also at a loss as to what the OP is trying to achieve. It took some back and forth to get the question into its current state. Decided to leave well-enough alone.

– Niall Cosgrove
Nov 22 '18 at 20:30





@WalterA I am also at a loss as to what the OP is trying to achieve. It took some back and forth to get the question into its current state. Decided to leave well-enough alone.

– Niall Cosgrove
Nov 22 '18 at 20:30












1 Answer
1






active

oldest

votes


















1














To access the filename that awk is currently processing use the builtin variable FILENAME



To bind other shell variables from your shell to variables in awk use:



awk -v var1=$shvar1 -v var2=$shvar2 'your awk code using var1 and var2'


Assuming files.txt contains your list of files and with zero understanding of what exactly you are trying to achieve:



for file_name in $(cat files.txt)
do
echo "progressing with file :${file_name}"
awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $file_name | uniq >> OUTPUT_FILE
done


I removed the cat and incorporated the grep into your awk. The cat was unnecessary since awk can read the file itself.



You can remove the for loop entirely by saying



awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $(<files.txt) | uniq >> OUTPUT_FILE


The $(<files.txt) will send each filename to awk.






share|improve this answer


























  • Here, we need to traverse all the 4 files. i.e, z5.cap, z6.cap and so on. if you remove cat how we are traversing?

    – karthik
    Nov 21 '18 at 13:09











  • The way you have written it, read file_name gets the names from user input. I can modify it to read the names from the .txt file if you like.

    – Niall Cosgrove
    Nov 21 '18 at 13:12











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411222%2fhow-to-print-files-names-from-file-where-awk-is-selecting-values%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









1














To access the filename that awk is currently processing use the builtin variable FILENAME



To bind other shell variables from your shell to variables in awk use:



awk -v var1=$shvar1 -v var2=$shvar2 'your awk code using var1 and var2'


Assuming files.txt contains your list of files and with zero understanding of what exactly you are trying to achieve:



for file_name in $(cat files.txt)
do
echo "progressing with file :${file_name}"
awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $file_name | uniq >> OUTPUT_FILE
done


I removed the cat and incorporated the grep into your awk. The cat was unnecessary since awk can read the file itself.



You can remove the for loop entirely by saying



awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $(<files.txt) | uniq >> OUTPUT_FILE


The $(<files.txt) will send each filename to awk.






share|improve this answer


























  • Here, we need to traverse all the 4 files. i.e, z5.cap, z6.cap and so on. if you remove cat how we are traversing?

    – karthik
    Nov 21 '18 at 13:09











  • The way you have written it, read file_name gets the names from user input. I can modify it to read the names from the .txt file if you like.

    – Niall Cosgrove
    Nov 21 '18 at 13:12
















1














To access the filename that awk is currently processing use the builtin variable FILENAME



To bind other shell variables from your shell to variables in awk use:



awk -v var1=$shvar1 -v var2=$shvar2 'your awk code using var1 and var2'


Assuming files.txt contains your list of files and with zero understanding of what exactly you are trying to achieve:



for file_name in $(cat files.txt)
do
echo "progressing with file :${file_name}"
awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $file_name | uniq >> OUTPUT_FILE
done


I removed the cat and incorporated the grep into your awk. The cat was unnecessary since awk can read the file itself.



You can remove the for loop entirely by saying



awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $(<files.txt) | uniq >> OUTPUT_FILE


The $(<files.txt) will send each filename to awk.






share|improve this answer


























  • Here, we need to traverse all the 4 files. i.e, z5.cap, z6.cap and so on. if you remove cat how we are traversing?

    – karthik
    Nov 21 '18 at 13:09











  • The way you have written it, read file_name gets the names from user input. I can modify it to read the names from the .txt file if you like.

    – Niall Cosgrove
    Nov 21 '18 at 13:12














1












1








1







To access the filename that awk is currently processing use the builtin variable FILENAME



To bind other shell variables from your shell to variables in awk use:



awk -v var1=$shvar1 -v var2=$shvar2 'your awk code using var1 and var2'


Assuming files.txt contains your list of files and with zero understanding of what exactly you are trying to achieve:



for file_name in $(cat files.txt)
do
echo "progressing with file :${file_name}"
awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $file_name | uniq >> OUTPUT_FILE
done


I removed the cat and incorporated the grep into your awk. The cat was unnecessary since awk can read the file itself.



You can remove the for loop entirely by saying



awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $(<files.txt) | uniq >> OUTPUT_FILE


The $(<files.txt) will send each filename to awk.






share|improve this answer















To access the filename that awk is currently processing use the builtin variable FILENAME



To bind other shell variables from your shell to variables in awk use:



awk -v var1=$shvar1 -v var2=$shvar2 'your awk code using var1 and var2'


Assuming files.txt contains your list of files and with zero understanding of what exactly you are trying to achieve:



for file_name in $(cat files.txt)
do
echo "progressing with file :${file_name}"
awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $file_name | uniq >> OUTPUT_FILE
done


I removed the cat and incorporated the grep into your awk. The cat was unnecessary since awk can read the file itself.



You can remove the for loop entirely by saying



awk -F' ' '($5 == "DEST") && ($3=="PAT210") { print FILENAME, $1}' $(<files.txt) | uniq >> OUTPUT_FILE


The $(<files.txt) will send each filename to awk.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 20:24

























answered Nov 21 '18 at 13:01









Niall CosgroveNiall Cosgrove

1,1681923




1,1681923













  • Here, we need to traverse all the 4 files. i.e, z5.cap, z6.cap and so on. if you remove cat how we are traversing?

    – karthik
    Nov 21 '18 at 13:09











  • The way you have written it, read file_name gets the names from user input. I can modify it to read the names from the .txt file if you like.

    – Niall Cosgrove
    Nov 21 '18 at 13:12



















  • Here, we need to traverse all the 4 files. i.e, z5.cap, z6.cap and so on. if you remove cat how we are traversing?

    – karthik
    Nov 21 '18 at 13:09











  • The way you have written it, read file_name gets the names from user input. I can modify it to read the names from the .txt file if you like.

    – Niall Cosgrove
    Nov 21 '18 at 13:12

















Here, we need to traverse all the 4 files. i.e, z5.cap, z6.cap and so on. if you remove cat how we are traversing?

– karthik
Nov 21 '18 at 13:09





Here, we need to traverse all the 4 files. i.e, z5.cap, z6.cap and so on. if you remove cat how we are traversing?

– karthik
Nov 21 '18 at 13:09













The way you have written it, read file_name gets the names from user input. I can modify it to read the names from the .txt file if you like.

– Niall Cosgrove
Nov 21 '18 at 13:12





The way you have written it, read file_name gets the names from user input. I can modify it to read the names from the .txt file if you like.

– Niall Cosgrove
Nov 21 '18 at 13:12




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411222%2fhow-to-print-files-names-from-file-where-awk-is-selecting-values%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini