Sponsored Content
Top Forums Shell Programming and Scripting awk or sed for finding closest pattern to a line number Post 302245704 by alirezan on Friday 10th of October 2008 06:10:44 PM
Old 10-10-2008
awk or sed for finding closest pattern to a line number

hi guys,

I want to do pattern matching with awk or sed but I don't know how. here's what I want:

I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk between "---" and that line number. But here's the catch. the pattern "---" can be between 1 and 20 lines above that line number and I don't know how many lines above ... Also, another catch is that, "---" can happen multiple times in the file but I want the "CLOSEST" to the line number. Here's an example:

Here's the file:

Quote:
---
aaa
bbb
ccc
ddd

---
aaaa
bbbb
cccc
dddd
eeee
ffff
gggg
hhhh

---
jjj
dsf
qqq
www
eee
I found the pattern say qqq to be on line 20. I want to find the first --- before pattern qqq (line 20) and I want to print the entire chunk from --- till the qqq pattern printed out.

Can someone help me out here please?
Thanks
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

2. Shell Programming and Scripting

Include Line Before Pattern Using Sed / Awk

Hi, I have a sql file that runs something like this vi Test.sql REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM PUBLIC; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM DEMO_READ; REVOKE SELECT ON DEMO_USER.DEMO_NOMINEE_TEST FROM DEMO_READ; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM... (3 Replies)
Discussion started by: rajan_san
3 Replies

3. UNIX for Advanced & Expert Users

sed match closest/nearest pattern

All i am struggling to raplace some text in a line between two (closest) patterns , line="/home/usr/bin/:/home/usr/devuser,n1.9/bin:/home/usr/root/bin" i want to replace "devuser,n1.9" with "NEWVAL", basically all teh text from "devuser" until nearest '/' with some new text. i tried teh... (1 Reply)
Discussion started by: sudheer1984
1 Replies

4. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

5. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

6. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

7. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

8. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

9. Shell Programming and Scripting

Print line when closest number if found

so i have a code that identifies which value is the closest to a value provided by the user. awk -F"," -v c=${COLUMN} -v t=${USTIME} '{a=$c}END{ asort(a);d=a-t;d=d<0?-d:d;v = a for(i=NR-1;i>=1;i--){ m=a-t;m=m<0?-m:m if(m<d){ ... (3 Replies)
Discussion started by: SkySmart
3 Replies

10. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies
fmlgrep(1F)							   FMLI Commands						       fmlgrep(1F)

NAME
fmlgrep - search a file for a pattern SYNOPSIS
fmlgrep [-b] [-c] [-i] [-l] [-n] [-s] [-v] limited_regular_expression [filename...] DESCRIPTION
fmlgrep searches filename for a pattern and prints all lines that contain that pattern. fmlgrep uses limited regular expressions (expres- sions that have string values that use a subset of the possible alphanumeric and special characters) like those described on the regexp(5) manual page to match the patterns. It uses a compact non-deterministic algorithm. Be careful when using FMLI special characters (for instance, $, `, ', ") in limited_regular_expression. It is safest to enclose the entire limited_regular_expression in single quotes ' ... '. If filename is not specified, fmlgrep assumes standard input. Normally, each line matched is copied to standard output. The file name is printed before each line matched if there is more than one input file. OPTIONS
The following options are supported: -b Precede each line by the block number on which it was found. This can be useful in locating block numbers by context (first block is 0). -c Print only a count of the lines that contain the pattern. -i Ignore upper/lower case distinction during comparisons. -l Print only the names of files with matching lines, separated by new-lines. Does not repeat the names of files when the pattern is found more than once. -n Precede each line by its line number in the file (first line is 1). -s Suppress error messages about nonexistent or unreadable files. -v Print all lines except those that contain the pattern. EXIT STATUS
The following exit values are returned: 0 if the pattern is found (that is, TRUE) 1 if the pattern is not found (that is, FALSE) 2 if an invalid expression was used or filename is inaccessible ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
egrep(1), fgrep(1), fmlcut(1F), grep(1), attributes(5), regexp(5) NOTES
Lines are limited to BUFSIZ characters; longer lines are truncated. BUFSIZ is defined in /usr/include/stdio.h. If there is a line with embedded nulls, fmlgrep will only match up to the first null; if it matches, it will print the entire line. SunOS 5.10 28 Mar 1995 fmlgrep(1F)
All times are GMT -4. The time now is 04:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy