LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-27-2019, 01:36 AM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209
Blog Entries: 1

Rep: Reputation: Disabled
How to check if a keyword is in a part of file?


I have a file with a pattern as like as below:

...
Start block
32
56
4545
89
23
End block
...
kdshf
ksdfdk
89
jdfh
...

I need to check if 89 is exist just in the block(start to end). I don't want to find 89 after the block.
How can I do this by sed or grep or awk in a script?
--------
New request:
Indeed I need to change the line: commentting some of items just in the block. My ideal output is a thing as below:
...
Start block
32
#56
#4545
#89

23
End block
...
kdshf
ksdfdk
56
4545
89


jdfh
...

Last edited by massy; 11-27-2019 at 11:24 PM.
 
Old 11-27-2019, 02:15 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
There are several ways to get there.

Code:
sed -n -e '/^Start/,/^End/{/^89$/p}' massy-02.data

test 89 -eq $(sed -n -e '/^Start/,/^End/{/^89$/p}' file.data ) && echo OK || echo Not
 
2 members found this post helpful.
Old 11-27-2019, 07:28 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by massy View Post
Please tell me whenever I'm wrong in English writing...
I am not a native speaker myself, so take this with a grain of salt.
Quote:
I have a file with a pattern as like as below:
with a pattern as below. No need for as like, and as like doesn't make much sense at this point anyway.
Quote:
I need to check if 89 is exist just in the block(start to end).
... if 89 exists ...
Quote:
How can I do this by sed or grep or awk in a script?
I would use with instead of by, but I am not certain if by is incorrect here.

Regarding the technical answer, you could use a more complicated awk program, but Turbocapitalist's solution is probably the most succinct one.

Last edited by berndbausch; 11-27-2019 at 07:29 AM.
 
1 members found this post helpful.
Old 11-27-2019, 08:16 AM   #4
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
There are several ways to get there.

Code:
sed -n -e '/^Start/,/^End/{/^89$/p}' massy-02.data

test 89 -eq $(sed -n -e '/^Start/,/^End/{/^89$/p}' file.data ) && echo OK || echo Not
Now I need to change the line: commentting some of items just in the block. My ideal output is a thing as below:
...
Start block
32
#56
#4545
#89

23
End block
...
kdshf
ksdfdk
56
4545
89


jdfh
...

Last edited by massy; 11-27-2019 at 08:25 AM.
 
Old 11-27-2019, 08:22 AM   #5
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
I am not a native speaker myself, so take this with a grain of salt.

with a pattern as below. No need for as like, and as like doesn't make much sense at this point anyway.

... if 89 exists ...

I would use with instead of by, but I am not certain if by is incorrect here.

Regarding the technical answer, you could use a more complicated awk program, but Turbocapitalist's solution is probably the most succinct one.

Thanks a lot :-).
 
Old 11-30-2019, 06:56 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Written as a multi-liner
Code:
sed '/^Start block/,/^End block/{
# we are within the block
# if the line is 89 then put a # at the beginning
/^89$/ s/^/#/
# if the line is 4545 then ...
/^4545$/ s/^/#/
}' input_file
With -i option you can write back to the input_file:
Code:
sed -i.bak ...
The (optional) .bak leaves a input_file.bak behind.

Last edited by MadeInGermany; 11-30-2019 at 07:07 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Read the folder name from file and check a keyword in one of the file inside that richa07 Linux - Newbie 4 06-28-2013 11:06 AM
Installing ubuntu 10.10 by usb : Unknown keyword in conf file daudiam Linux - Laptop and Netbook 2 03-26-2011 01:05 PM
how can i add a keyword after the field2 in a log file rdhanek Linux - General 1 07-29-2009 09:28 AM
"Unknown keyword in config file" when trying to boot from CD jkh107 Fedora - Installation 7 03-26-2009 12:29 AM
how to grep a "keyword" beside another keyword? xiawinter Linux - Software 7 12-29-2007 12:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration