awk/sed match and extraction


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk/sed match and extraction
# 1  
Old 09-21-2012
awk/sed match and extraction

Hi,

I have a file like this-

Code:
aa
12
23
34
aa
21
34
56
aa
78
45
56

I want to print out only the lines after the last aa. How do I do this? I tried using grep -A and sed -n, but both didnt work as I wanted to.
Could someone help me out please..
# 2  
Old 09-21-2012
try this..
Code:
$awk '{if($0 ~ /aa/) {s="";getline;s=$0} else{s=s"\n"$0}}END{print s}' file
78
45
56

This User Gave Thanks to pamu For This Post:
# 3  
Old 09-21-2012
it works Smilie thanks!
# 4  
Old 09-21-2012
This should also work correctly if there are no lines with "aa":
Code:
awk 'f{p=p $0 RS} /^aa$/{p=x; f=1} END{if(f)print p}' ORS= infile

This User Gave Thanks to Scrutinizer 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

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

2. Shell Programming and Scripting

Pattern match with awk/sed - help

I need to grep for the pattern text inside the square brackets which are in red and not in green..my current code greps patterns both of them, which i don't want Input fileref|XP_002371341.1| oxoacyl-ACP reductase, putative gb|EPT24759.1| 3-ketoacyl-(acyl-carrier-protein) reductase ... (2 Replies)
Discussion started by: selvankj
2 Replies

3. Shell Programming and Scripting

Replace second match+awk/sed

I have a text file that looks like this: ----------------------------------------- sta WP00 time 10/23/2013 20:10:17 sensor trillium_240_2 0 583 add close sensor trillium_240_2 10/23/2013 20:10:17 sensor trillium_120 0 279 add close sensor trillium_120 10/23/2013 20:10:35... (11 Replies)
Discussion started by: klane
11 Replies

4. UNIX for Advanced & Expert Users

Column Extraction for a particular match

Hi, PFB the input: unix/java/perl/random.txt unix1/java1/random1.txt unix2/java2/perl2/random2.txt unix3/java3/random3.txt unix4/random4.txt i want the following output: random.txt random1.txt random2.txt random3.txt random4.txt the patterns can change but i need the .txt file... (5 Replies)
Discussion started by: arindam guha
5 Replies

5. 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

6. Shell Programming and Scripting

awk or sed? change field conditional on key match

Hi. I'd appreciate if I can get some direction in this issue to get me going. Datafile1: -About 4000 records, I have to update field#4 in selected records based on a match in the key field (Field#1). -Field #1 is the key field (servername) . # of Fields may vary # comment server1 bbb ccc... (2 Replies)
Discussion started by: RascalHoudi
2 Replies

7. Shell Programming and Scripting

match string exactly with awk/sed

Hi all, I have a list that I would like to parse with awk/sed. The list is contains entries such as: JournalTitle: Biochemistry JournalTitle: Biochemistry and cell biology = Biochimie et biologie cellulaire JournalTitle: Biochemistry and experimental biology JournalTitle: Biochemistry and... (6 Replies)
Discussion started by: euval
6 Replies

8. Shell Programming and Scripting

awk/sed to extract column bases on partial match

Hi I have a log file which has outputs like the one below conn=24,196 op=1 RESULT err=0 tag=0 nentries=9 etime=3,712 dbtime=0 mem=486,183,328/2,147,483,648 Now most of the time I am only interested in the time ( the first column) and a column that begins with etime i.e... (8 Replies)
Discussion started by: pkabali
8 Replies

9. Shell Programming and Scripting

Extraction of text using sed or awk command

Hi All, I need to extract 543 from the command below : # pvscan PV /dev/sdb1 VG vg0 lvm2 Total: 1 543.88 GB] / in use: 1 / in no VG: 0 I have the following command which does the job, but I think this could be achieved in a more simple way using sed or awk. Any help is... (7 Replies)
Discussion started by: nua7
7 Replies

10. Shell Programming and Scripting

Sed/awk gods, I need your Help! Fancy log extraction

Hi! I'm trying to find a way to extract a certain amount of lines from a log file. This would allow me to "follow" a web user through our log files. Here is a sample fake log file to explain what i want to accomplish : BEGIN REQUEST sessionID=123456 boatload of lines for thread-1 detailing... (8 Replies)
Discussion started by: gnagus
8 Replies
Login or Register to Ask a Question