search a string and print the string and it's header
I need to search for a string (number here), and print it's header or title using grep or awk or anything.
please check this example:
Input file content:
#####
Production_Broad:
#####
678
544
#####
IGHTY_BBBT:
#####
1666
2515
2516
2517
2518
#####
Jaguar:
#####
280
#####
Loyalty:
#####
5179
#####
MC_Addr:
#####
544
577890
#####
erce_Ban_1:
#####
7455
5656
I want to search for the number "2515", and it gives me below output:
IGHTY_BBBT:
2515
and if i searched for "5179", the output should be as below:
Loyalty:
5179
linux shell-script
add a comment |
I need to search for a string (number here), and print it's header or title using grep or awk or anything.
please check this example:
Input file content:
#####
Production_Broad:
#####
678
544
#####
IGHTY_BBBT:
#####
1666
2515
2516
2517
2518
#####
Jaguar:
#####
280
#####
Loyalty:
#####
5179
#####
MC_Addr:
#####
544
577890
#####
erce_Ban_1:
#####
7455
5656
I want to search for the number "2515", and it gives me below output:
IGHTY_BBBT:
2515
and if i searched for "5179", the output should be as below:
Loyalty:
5179
linux shell-script
This is really not that hard. What have you tried?
– Tigger
May 15 '17 at 12:01
I tried this command after putting source file "out" and required numbers in file list "for i incat list
;do if [[grep -w $i out
]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.
– Fady.Fouad
May 15 '17 at 12:03
add a comment |
I need to search for a string (number here), and print it's header or title using grep or awk or anything.
please check this example:
Input file content:
#####
Production_Broad:
#####
678
544
#####
IGHTY_BBBT:
#####
1666
2515
2516
2517
2518
#####
Jaguar:
#####
280
#####
Loyalty:
#####
5179
#####
MC_Addr:
#####
544
577890
#####
erce_Ban_1:
#####
7455
5656
I want to search for the number "2515", and it gives me below output:
IGHTY_BBBT:
2515
and if i searched for "5179", the output should be as below:
Loyalty:
5179
linux shell-script
I need to search for a string (number here), and print it's header or title using grep or awk or anything.
please check this example:
Input file content:
#####
Production_Broad:
#####
678
544
#####
IGHTY_BBBT:
#####
1666
2515
2516
2517
2518
#####
Jaguar:
#####
280
#####
Loyalty:
#####
5179
#####
MC_Addr:
#####
544
577890
#####
erce_Ban_1:
#####
7455
5656
I want to search for the number "2515", and it gives me below output:
IGHTY_BBBT:
2515
and if i searched for "5179", the output should be as below:
Loyalty:
5179
linux shell-script
linux shell-script
edited 34 mins ago
Rui F Ribeiro
40.1k1479135
40.1k1479135
asked May 15 '17 at 11:56
Fady.FouadFady.Fouad
61
61
This is really not that hard. What have you tried?
– Tigger
May 15 '17 at 12:01
I tried this command after putting source file "out" and required numbers in file list "for i incat list
;do if [[grep -w $i out
]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.
– Fady.Fouad
May 15 '17 at 12:03
add a comment |
This is really not that hard. What have you tried?
– Tigger
May 15 '17 at 12:01
I tried this command after putting source file "out" and required numbers in file list "for i incat list
;do if [[grep -w $i out
]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.
– Fady.Fouad
May 15 '17 at 12:03
This is really not that hard. What have you tried?
– Tigger
May 15 '17 at 12:01
This is really not that hard. What have you tried?
– Tigger
May 15 '17 at 12:01
I tried this command after putting source file "out" and required numbers in file list "for i in
cat list
;do if [[ grep -w $i out
]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.– Fady.Fouad
May 15 '17 at 12:03
I tried this command after putting source file "out" and required numbers in file list "for i in
cat list
;do if [[ grep -w $i out
]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.– Fady.Fouad
May 15 '17 at 12:03
add a comment |
3 Answers
3
active
oldest
votes
If headers are identified by those #####
lines, and the list of IDs to search for is in a ids.txt
file (one per line), you could do:
awk '
!ids_processed{ids[$0]; next}
$0 == "#####" {getline header; getline; next}
$0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt
For instance, if ids.txt
contains
2515
544
577890
On your sample, that would give:
Production_Broad:
544
IGHTY_BBBT:
2515
MC_Addr:
544
MC_Addr:
577890
add a comment |
You can try something like this:
awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt
Where "2516" is the number you are searching for.
obs: I'm assuming that all the labels ends with ":".
I hope this helps.
Can i put the numbers i am searching for in a file (not only 2516)??
– Fady.Fouad
May 15 '17 at 12:27
add a comment |
awk approach:
awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{
getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file
The output:
IGHTY_BBBT:
2515
RS="#####"
- treating #####
as record separator
-v num=2515
- a variable containing search number
$0~/[0-9a-zA-Z_]+:/
- capturing header line
getline nl
- get next record containing numbers
if (nl~num)
- if search number is matched within a line of numbers
Can i put the numbers i am searching for in a file (not only 2515) ??
– Fady.Fouad
May 15 '17 at 12:32
@Fady.Fouad, do you want to search by multiple numbers?
– RomanPerekhrest
May 15 '17 at 12:34
Yes by multiple numbers in a file
– Fady.Fouad
May 15 '17 at 13:30
@Fady.Fouad, and how they will be separated, by space or by newline within a file?
– RomanPerekhrest
May 15 '17 at 13:32
By new line within file.
– Fady.Fouad
May 15 '17 at 14:40
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%2f365160%2fsearch-a-string-and-print-the-string-and-its-header%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
If headers are identified by those #####
lines, and the list of IDs to search for is in a ids.txt
file (one per line), you could do:
awk '
!ids_processed{ids[$0]; next}
$0 == "#####" {getline header; getline; next}
$0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt
For instance, if ids.txt
contains
2515
544
577890
On your sample, that would give:
Production_Broad:
544
IGHTY_BBBT:
2515
MC_Addr:
544
MC_Addr:
577890
add a comment |
If headers are identified by those #####
lines, and the list of IDs to search for is in a ids.txt
file (one per line), you could do:
awk '
!ids_processed{ids[$0]; next}
$0 == "#####" {getline header; getline; next}
$0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt
For instance, if ids.txt
contains
2515
544
577890
On your sample, that would give:
Production_Broad:
544
IGHTY_BBBT:
2515
MC_Addr:
544
MC_Addr:
577890
add a comment |
If headers are identified by those #####
lines, and the list of IDs to search for is in a ids.txt
file (one per line), you could do:
awk '
!ids_processed{ids[$0]; next}
$0 == "#####" {getline header; getline; next}
$0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt
For instance, if ids.txt
contains
2515
544
577890
On your sample, that would give:
Production_Broad:
544
IGHTY_BBBT:
2515
MC_Addr:
544
MC_Addr:
577890
If headers are identified by those #####
lines, and the list of IDs to search for is in a ids.txt
file (one per line), you could do:
awk '
!ids_processed{ids[$0]; next}
$0 == "#####" {getline header; getline; next}
$0 in ids {print header ORS $0}' ids.txt ids_processed=1 input.txt
For instance, if ids.txt
contains
2515
544
577890
On your sample, that would give:
Production_Broad:
544
IGHTY_BBBT:
2515
MC_Addr:
544
MC_Addr:
577890
edited May 15 '17 at 15:38
answered May 15 '17 at 12:38
Stéphane ChazelasStéphane Chazelas
305k57576930
305k57576930
add a comment |
add a comment |
You can try something like this:
awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt
Where "2516" is the number you are searching for.
obs: I'm assuming that all the labels ends with ":".
I hope this helps.
Can i put the numbers i am searching for in a file (not only 2516)??
– Fady.Fouad
May 15 '17 at 12:27
add a comment |
You can try something like this:
awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt
Where "2516" is the number you are searching for.
obs: I'm assuming that all the labels ends with ":".
I hope this helps.
Can i put the numbers i am searching for in a file (not only 2516)??
– Fady.Fouad
May 15 '17 at 12:27
add a comment |
You can try something like this:
awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt
Where "2516" is the number you are searching for.
obs: I'm assuming that all the labels ends with ":".
I hope this helps.
You can try something like this:
awk '{if(match($0,":")) header=$0; if($0 == 2516) printf("%sn%sn",header,$0)}' input_file.txt
Where "2516" is the number you are searching for.
obs: I'm assuming that all the labels ends with ":".
I hope this helps.
answered May 15 '17 at 12:08
Daniel VasconcelosDaniel Vasconcelos
1665
1665
Can i put the numbers i am searching for in a file (not only 2516)??
– Fady.Fouad
May 15 '17 at 12:27
add a comment |
Can i put the numbers i am searching for in a file (not only 2516)??
– Fady.Fouad
May 15 '17 at 12:27
Can i put the numbers i am searching for in a file (not only 2516)??
– Fady.Fouad
May 15 '17 at 12:27
Can i put the numbers i am searching for in a file (not only 2516)??
– Fady.Fouad
May 15 '17 at 12:27
add a comment |
awk approach:
awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{
getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file
The output:
IGHTY_BBBT:
2515
RS="#####"
- treating #####
as record separator
-v num=2515
- a variable containing search number
$0~/[0-9a-zA-Z_]+:/
- capturing header line
getline nl
- get next record containing numbers
if (nl~num)
- if search number is matched within a line of numbers
Can i put the numbers i am searching for in a file (not only 2515) ??
– Fady.Fouad
May 15 '17 at 12:32
@Fady.Fouad, do you want to search by multiple numbers?
– RomanPerekhrest
May 15 '17 at 12:34
Yes by multiple numbers in a file
– Fady.Fouad
May 15 '17 at 13:30
@Fady.Fouad, and how they will be separated, by space or by newline within a file?
– RomanPerekhrest
May 15 '17 at 13:32
By new line within file.
– Fady.Fouad
May 15 '17 at 14:40
add a comment |
awk approach:
awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{
getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file
The output:
IGHTY_BBBT:
2515
RS="#####"
- treating #####
as record separator
-v num=2515
- a variable containing search number
$0~/[0-9a-zA-Z_]+:/
- capturing header line
getline nl
- get next record containing numbers
if (nl~num)
- if search number is matched within a line of numbers
Can i put the numbers i am searching for in a file (not only 2515) ??
– Fady.Fouad
May 15 '17 at 12:32
@Fady.Fouad, do you want to search by multiple numbers?
– RomanPerekhrest
May 15 '17 at 12:34
Yes by multiple numbers in a file
– Fady.Fouad
May 15 '17 at 13:30
@Fady.Fouad, and how they will be separated, by space or by newline within a file?
– RomanPerekhrest
May 15 '17 at 13:32
By new line within file.
– Fady.Fouad
May 15 '17 at 14:40
add a comment |
awk approach:
awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{
getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file
The output:
IGHTY_BBBT:
2515
RS="#####"
- treating #####
as record separator
-v num=2515
- a variable containing search number
$0~/[0-9a-zA-Z_]+:/
- capturing header line
getline nl
- get next record containing numbers
if (nl~num)
- if search number is matched within a line of numbers
awk approach:
awk -v RS="#####" -v num=2515 '$0~/[0-9a-zA-Z_]+:/{
getline nl; if (nl~num){sub("n", "", $0); printf("%s%dn",$0,num)}}' file
The output:
IGHTY_BBBT:
2515
RS="#####"
- treating #####
as record separator
-v num=2515
- a variable containing search number
$0~/[0-9a-zA-Z_]+:/
- capturing header line
getline nl
- get next record containing numbers
if (nl~num)
- if search number is matched within a line of numbers
answered May 15 '17 at 12:19
RomanPerekhrestRomanPerekhrest
23k12447
23k12447
Can i put the numbers i am searching for in a file (not only 2515) ??
– Fady.Fouad
May 15 '17 at 12:32
@Fady.Fouad, do you want to search by multiple numbers?
– RomanPerekhrest
May 15 '17 at 12:34
Yes by multiple numbers in a file
– Fady.Fouad
May 15 '17 at 13:30
@Fady.Fouad, and how they will be separated, by space or by newline within a file?
– RomanPerekhrest
May 15 '17 at 13:32
By new line within file.
– Fady.Fouad
May 15 '17 at 14:40
add a comment |
Can i put the numbers i am searching for in a file (not only 2515) ??
– Fady.Fouad
May 15 '17 at 12:32
@Fady.Fouad, do you want to search by multiple numbers?
– RomanPerekhrest
May 15 '17 at 12:34
Yes by multiple numbers in a file
– Fady.Fouad
May 15 '17 at 13:30
@Fady.Fouad, and how they will be separated, by space or by newline within a file?
– RomanPerekhrest
May 15 '17 at 13:32
By new line within file.
– Fady.Fouad
May 15 '17 at 14:40
Can i put the numbers i am searching for in a file (not only 2515) ??
– Fady.Fouad
May 15 '17 at 12:32
Can i put the numbers i am searching for in a file (not only 2515) ??
– Fady.Fouad
May 15 '17 at 12:32
@Fady.Fouad, do you want to search by multiple numbers?
– RomanPerekhrest
May 15 '17 at 12:34
@Fady.Fouad, do you want to search by multiple numbers?
– RomanPerekhrest
May 15 '17 at 12:34
Yes by multiple numbers in a file
– Fady.Fouad
May 15 '17 at 13:30
Yes by multiple numbers in a file
– Fady.Fouad
May 15 '17 at 13:30
@Fady.Fouad, and how they will be separated, by space or by newline within a file?
– RomanPerekhrest
May 15 '17 at 13:32
@Fady.Fouad, and how they will be separated, by space or by newline within a file?
– RomanPerekhrest
May 15 '17 at 13:32
By new line within file.
– Fady.Fouad
May 15 '17 at 14:40
By new line within file.
– Fady.Fouad
May 15 '17 at 14:40
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%2f365160%2fsearch-a-string-and-print-the-string-and-its-header%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
This is really not that hard. What have you tried?
– Tigger
May 15 '17 at 12:01
I tried this command after putting source file "out" and required numbers in file list "for i in
cat list
;do if [[grep -w $i out
]];then grep -w $i out;else echo $i" :not found";fi ;done", but i got no headers.– Fady.Fouad
May 15 '17 at 12:03