awk and greping between lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk and greping between lines
# 1  
Old 09-25-2016
awk and greping between lines

i want to grab lines from a file that are between two patterns (including the lines that contain the pattern).

here's what im currently doing:

data.txt
Code:
aaa
bbb
cccc
ddd
eee
ffff
ddd

code:
Code:
awk '/bbb/,/fff/ && $0 !~ /ddd/' cdsnmp.sh

I want to grab lines between and including bbb and ffff, but i want to avoid the lines containing ddd
# 2  
Old 09-25-2016
Hello SkySmart,

Could you please try following and let me know if this helps you.
Code:
awk '($0 ~ /ddd/){next} ($0 ~ /bbb/){A=1} {if(A){print}} ($0 ~ /ffff/){A=0}'  Input_file

Output will be as follows.
Code:
bbb
cccc
eee
ffff

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-25-2016
Try (which I think is simplest / the most straight forward):
Code:
awk '/bbb/,/fff/{if(!/ddd/) print}' file

or otherwise:
Code:
awk '/bbb/{p=1} p && !/ddd/; /fff/{p=0}' file


----
@ravindersingh: your approach could be further reduced to:
Code:
awk '/ddd/{next} /bbb/{A=1}A; /ffff/{A=0}' file

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 09-25-2016
In sed:
Code:
 sed -ne '/ddd/d;/bbb/,/fff/p' file

This User Gave Thanks to greet_sed For This Post:
# 5  
Old 09-27-2016
Hi.

Also:
Code:
perl -wn -e 'if ( /bbb/../fff/ ) { print unless /ddd/ } ;' input-file

producing:
Code:
bbb
cccc
eee
ffff

On a system:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
perl 5.20.2

Best wishes ... cheers, drl
# 6  
Old 09-27-2016
The shortest awk resolution:

Code:
$ awk '/ddd/{next}/aaa/,/fff/' data.txt
aaa
bbb
cccc
eee
ffff

This User Gave Thanks to blastit.fr For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Beginners Questions & Answers

Qn on awk greping values

Hello Experts, I was trying to awk out some data out of a text file. Below is a sample file which I have xxx ***Wed Jun 28 18:00:59 CDT 2015 avg-cpu: %user %nice %system %iowait %steal %idle 17.10 0.00 4.56 2.86 0.00 75.48 Device: rrqm/s wrqm/s ... (2 Replies)
Discussion started by: DevAnand
2 Replies

3. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

4. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

5. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

6. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

7. Shell Programming and Scripting

Greping the column with a value greater than 50 with awk

Hi All, I am running a command from a remote server using ssh to different servers. I will get a output like below with 4 columns. I want to grab line which is having a coulmn which grate than or equal to 50. How can I do it with Awk or sed ??. I add a space to first row with sed 's/::/:: /g' to... (4 Replies)
Discussion started by: raghin
4 Replies

8. Shell Programming and Scripting

Greping in between two different lines.

I know you could use the grep "something" -A200 flag to get all the lines past your pattern. Is there a way to get all the lines in between two patterns? The -a flag will not work since all lines in between the two patterns don't have a constant number. (4 Replies)
Discussion started by: jwillis0720
4 Replies

9. Shell Programming and Scripting

problem in greping the string from file using awk

I am using awk command for greping an value from the file the file contains .. file ---------------------------- content----------- -------- String main = "81507066666"; ------------------------------ i am greping the above value using awk command NumberToReplace=`cat "file" | grep... (1 Reply)
Discussion started by: vastare
1 Replies

10. Shell Programming and Scripting

Greping certain lines

One of my outout is like this as shown below. How can I grep only the lines after the line "Affected files ...". No of lines after the line "Affected files ..." may vary. $ cat file_A Change 149133 by csaha@test_depo_csaha on 2006/02/08 01:40:57 *pending* This is to test change #... (5 Replies)
Discussion started by: csaha
5 Replies
Login or Register to Ask a Question