awk/sed/perl command to delete specific pattern and content above it...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk/sed/perl command to delete specific pattern and content above it...
# 1  
Old 07-05-2010
awk/sed/perl command to delete specific pattern and content above it...

Hi,

Below is my input file:
Code:
Data: 1
Length: 20



Got result.

Data: 2
Length: 30



No result.

Data: 3
Length: 20



Got result.

Data: 4
Length: 20



Got result.

Data: 5.5
Length: 20



Got result.

Desired output:
Code:
Data: 1
Length: 20



Got result.


Data: 3
Length: 20



Got result.

Data: 4
Length: 20



Got result.

Data: 5.5
Length: 20



Got result.

My purpose just want to delete based on specific pattern "No result." and file line before the pattern "No result."
I'm appreciate for any suggestion.
Thanks Smilie

Last edited by edge_diners; 07-05-2010 at 04:39 AM..
# 2  
Old 07-05-2010
Hi

Code:
awk '/result/{ if ($0 ~ /Got/){for(j=1;j<=i;j++)print a[j];print;}i=1;next;}{a[i++]=$0}' i=1 infile

But, it looks like the above sol misses some blank lines.

Guru.
# 3  
Old 07-05-2010
Code:
awk -vRS="." '!/No result/' file

# 4  
Old 07-05-2010
Hi,

Instead of based on "Got" pattern to extract, do you got any idea that allow me to delete my content based on "No result."?
The reason I hope can delete the line based on "No result." is because some of the "Got result" maybe got more line content.
Thanks again for any of your advice Smilie

---------- Post updated at 02:30 AM ---------- Previous update was at 02:27 AM ----------

Hi Bartus11,

I just try your command.
But it seems like it will affect some of the content of "Got result" due to the exist of "." in some content of "Got result".
Do you got any other suggestion to archive my goal?
Thanks first Smilie
# 5  
Old 07-05-2010
My suggestion is that you provide sample of the real input file.
# 6  
Old 07-05-2010
Hi, bartus11.
I just include those content that might affect by the command you suggested.
Thanks a lot for your advice Smilie
# 7  
Old 07-05-2010
Try
Code:
awk -vRS="D" -vORS="D" '!/No result/' file

This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to delete content before and after a matched pattern

Hello, I have been trying to write a script where I could get awk to delete data before and after a matched pattern. For eg Raw data Start NAME = John Age = 35 Occupation = Programmer City = New York Certification Completed = No Salary = 80000 End Start NAME = Mary Age = 25... (2 Replies)
Discussion started by: sidnow
2 Replies

3. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

4. Shell Programming and Scripting

Sorting content between match pattern and move on with awk and sed

S 0.0 0.0 (reg, inst050) k e f d c S 0.0 0.0 (mux, m030) k g r s x v S 0.0 0.0 (reg, inst020) q s n m (12 Replies)
Discussion started by: ctphua
12 Replies

5. Shell Programming and Scripting

sed command to delete a pattern in a file

Hi Everyone, I have an unusual requirement. Here is where i am stuck for sometime now... I have this text file.. lets say .. output.sql... it has lot of entries... here below is part of the entry... .. . . . . . . . . . . . . . . . .... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

7. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

8. Shell Programming and Scripting

How can i delete the content between all the occurences of two strings using sed or awk command

Hi. I have to delete the content between all the occurrences of the xml tags in a single file. For example: * The tags <script>.....................</script> occurs more than once in the same file. * It follows tagging rules meaning a start tag will be followed by an end tag. Will not have... (9 Replies)
Discussion started by: satheeshkumar
9 Replies

9. HP-UX

How to delete specific pattern in a file with SED?

I have one file which is having content as following... 0513468211,,,,20091208,084005,5,,2,3699310, 0206554475,,,,20090327,123634,85,,2,15615533 0206554475,,,,20090327,134431,554,,2,7246177 0103000300,,,,20090523,115501,89,,2,3869929 0736454328,,,,20091208,084005,75,,2,3699546... (7 Replies)
Discussion started by: ganesh.mandlik
7 Replies

10. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies
Login or Register to Ask a Question