LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2013, 07:25 PM   #1
igorza
LQ Newbie
 
Registered: Apr 2013
Posts: 2

Rep: Reputation: Disabled
Shell script to find a string and print x lines before and y lines after the string


I have a file like this:

-----------------------------------

Mode: 5

priority: 1
status: on
version: 1.0.1
description: it is ok
short description: ok
value: D
target: a23
-----------------------------------

Mode: 22

priority: 4
status: on
version: 1.3.1
description: it is the best
short description: the best
value: G
target: b13

-----------------------------------

Mode: 15

priority: 2
status: off
version: 2.0.3
description: it isn't ok
short description: nok
value: D
target: b43

-----------------------------------

Mode: 23

priority: 1
status: off
version: 3.2.3
description: description
short description: desc
value: A
target: f27

-----------------------------------


I want to save to file only parts/sections where value is D (value: D), so new file should be:
-----------------------------------

Mode: 5

priority: 1
status: on
version: 1.0.1
description: it is ok
short description: ok
value: D
target: a23
-----------------------------------

Mode: 15

priority: 2
status: off
version: 2.0.3
description: it isn't ok
short description: nok
value: D
target: b43

-----------------------------------

Idea is to find string »value: D« and to print this line, plus 9 lines above and 2 lines below, and search next »value: D« string… using awk or sed. The string is repeated many times in the file. Any help?
 
Old 04-17-2013, 08:38 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
grep -A1 -B9 --no-group-separator 'value: D' t.t
If you use -A2 with that data, you'll get duplicated '------------' lines.
EG your first data block has no blank line at the end, the others do.
If you give it a blank line, then -A2 is fine; see http://linux.die.net/man/1/grep
 
1 members found this post helpful.
Old 04-17-2013, 09:37 PM   #3
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
I've been searching for a way to get rid of the group separator for a while now. This is not documented in the manpages and does not show up with grep --help:

Code:
grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             print version information and exit
      --help                display this help and exit
      --mmap                ignored for backwards compatibility

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print the filename for each match
  -h, --no-filename         suppress the prefixing filename on output
      --label=LABEL         print LABEL as filename for standard input
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is `binary', `text', or `without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is `read', `recurse', or `skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is `read' or `skip'
  -R, -r, --recursive       equivalent to --directories=recurse
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
  -L, --files-without-match  print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is `always', `never', or `auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS)
  -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

`egrep' means `grep -E'.  `fgrep' means `grep -F'.
Direct invocation as either `egrep' or `fgrep' is deprecated.
With no FILE, or when FILE is -, read standard input.  If less than two FILEs
are given, assume -h.  Exit status is 0 if any line was selected, 1 otherwise;
if any error occurs and -q was not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU Grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Using GNU grep 2.6.3.
 
Old 04-17-2013, 10:35 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The wonders of Google
Somebody read the src & I found his post

Search "grep suppress group separator"

1st result http://stackoverflow.com/questions/2...-context-lines
 
Old 04-17-2013, 10:52 PM   #5
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Thanks. Did not get around to searching. I was trying to do it with sed also using http://www.grymoire.com/Unix/Sed.html#toc-uh-56.
 
Old 04-18-2013, 03:00 AM   #6
igorza
LQ Newbie
 
Registered: Apr 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you all.
 
Old 04-18-2013, 04:31 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The info pages of most commands have more detailed information than the man pages. You'll find the --no-group-separator listed there. There's also --group-separator=STRING' if you want to change it to something other than the default.

Do note that these are gnu grep extensions, and were only added fairly recently at that.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to print lines when debugging shell script shayke23 Linux - Software 3 03-07-2011 03:54 AM
find a string in a file and delete that line as well as x lines after tdnnash25 Linux - Server 3 06-24-2010 02:13 PM
[SOLVED] Delete all lines containing a string, plus 4 lines below it? RedHelix Linux - Newbie 4 01-27-2010 09:13 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:17 PM.

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