Sed or Awk for lines between two strings multiple times and keep the last one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed or Awk for lines between two strings multiple times and keep the last one
# 1  
Old 02-06-2012
Error Sed or Awk for lines between two strings multiple times and keep the last one

Hi,
I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g.
Standard
Some words
Some words
Some words
Visual

Standard
Some words
Some words
Visual

Standard
Some words
Some words
Some words
Some words
Visual
Some words
Some words

I am trying to get the highlighted portion

Thank you so much
Dave

Code:
Standard
Some words
Some words
Some words
Visual
 
Standard
Some words
Some words
Visual
 
Standard
Some words
Some words
Some words
Some words
Visual
Some words
Some words


Last edited by damanidada; 02-07-2012 at 04:13 AM.. Reason: Please wrap samples and scripts in CodeTags
# 2  
Old 02-06-2012
Highlighting often does not show

Please show an examples of file
original
desired output
This User Gave Thanks to joeyg For This Post:
# 3  
Old 02-06-2012
Code:
awk '/Standard/,/Visual/' inputfile | awk 'END{print $(NF-1)}' RS= FS="Standard|Visual"

If running on Solaris or SunOS plateform, just use nawk or /usr/xpg4/bin/awk instead of awk
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 02-06-2012
Code:
awk '
BEGIN {
  RS=""
  FS="\n"
}
$1=="Standard" && /Visual/ {
  s=$2
  for  (i=3 ; i<=NF ; ++i ) {
    if ( $i=="Visual" ) { break }
    s=sprintf("%s\n%s",s,$i)
  }
}
END {
  print s
}'

This User Gave Thanks to chihung For This Post:
# 5  
Old 02-07-2012
Quote:
Originally Posted by joeyg
Please show an examples of file
original
desired output
Thank you Joeyg,
I just noticed that the highlighting did not work earlier. I successeed in finally highlighting the desired text.

---------- Post updated at 03:42 AM ---------- Previous update was at 03:16 AM ----------

Quote:
Originally Posted by ctsgnb
Code:
awk '/Standard/,/Visual/' inputfile | awk 'END{print $(NF-1)}' RS= FS="Standard|Visual"

If running on Solaris or SunOS plateform, just use nawk or /usr/xpg4/bin/awk instead of awk
Thank you ctsgnb, the code worked.

---------- Post updated at 03:43 AM ---------- Previous update was at 03:42 AM ----------

Quote:
Originally Posted by ctsgnb
Code:
awk '/Standard/,/Visual/' inputfile | awk 'END{print $(NF-1)}' RS= FS="Standard|Visual"

If running on Solaris or SunOS plateform, just use nawk or /usr/xpg4/bin/awk instead of awk
Thank you ctsgnb, the code worked.

---------- Post updated at 04:28 AM ---------- Previous update was at 03:43 AM ----------

Quote:
Originally Posted by chihung
Code:
awk '
BEGIN {
  RS=""
  FS="\n"
}
$1=="Standard" && /Visual/ {
  s=$2
  for  (i=3 ; i<=NF ; ++i ) {
    if ( $i=="Visual" ) { break }
    s=sprintf("%s\n%s",s,$i)
  }
}
END {
  print s
}'

Thank you chihung, I will check this code out.

---------- Post updated at 04:30 AM ---------- Previous update was at 04:28 AM ----------

Quote:
Originally Posted by chihung
Code:
awk '
BEGIN {
  RS=""
  FS="\n"
}
$1=="Standard" && /Visual/ {
  s=$2
  for  (i=3 ; i<=NF ; ++i ) {
    if ( $i=="Visual" ) { break }
    s=sprintf("%s\n%s",s,$i)
  }
}
END {
  print s
}'

Thank you chihung, I will check this code out.

---------- Post updated at 05:08 AM ---------- Previous update was at 04:30 AM ----------

Quote:
Originally Posted by chihung
Code:
awk '
BEGIN {
  RS=""
  FS="\n"
}
$1=="Standard" && /Visual/ {
  s=$2
  for  (i=3 ; i<=NF ; ++i ) {
    if ( $i=="Visual" ) { break }
    s=sprintf("%s\n%s",s,$i)
  }
}
END {
  print s
}'

Thank you chihung, I will check out this code.

---------- Post updated at 05:09 AM ---------- Previous update was at 05:08 AM ----------

Quote:
Originally Posted by chihung
Code:
awk '
BEGIN {
  RS=""
  FS="\n"
}
$1=="Standard" && /Visual/ {
  s=$2
  for  (i=3 ; i<=NF ; ++i ) {
    if ( $i=="Visual" ) { break }
    s=sprintf("%s\n%s",s,$i)
  }
}
END {
  print s
}'

Thank you chihung, I will check out this code.

---------- Post updated at 05:11 AM ---------- Previous update was at 05:09 AM ----------

Quote:
Originally Posted by chihung
Code:
awk '
BEGIN {
  RS=""
  FS="\n"
}
$1=="Standard" && /Visual/ {
  s=$2
  for  (i=3 ; i<=NF ; ++i ) {
    if ( $i=="Visual" ) { break }
    s=sprintf("%s\n%s",s,$i)
  }
}
END {
  print s
}'



Thank you chihung, I will check out this code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find=grep or maybe sed/awk for multiple lines of text?

Hi, I am running the following: PASS="username/password" sqlplus -s << EOF | grep -v "^$" $PASS set feedback off set heading off set termout off select name from v\$database ; exit EOF Which gives ERROR: ORA-28002: the password will expire within 5 days PSMP1 (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Sed/awk/perl substitution with multiple lines

OSX I have been grinding my teeth on a portion of code. I am building a bash script that edits a html email template. In the template, I have place holders for SED (or whatever program is appropriate) to use as anchors for find and replace, with user defined corresponding html code. The HTML code... (3 Replies)
Discussion started by: sudo
3 Replies

3. Shell Programming and Scripting

Awk/sed : help on:Filtering multiple lines to one:

Experts Good day, I want to filter multiple lines of same error of same day , to only 1 error of each day, the first line from the log. Here is the file: May 26 11:29:19 cmihpx02 vmunix: NFS write failed for server cmiauxe1: error 5 (RPC: Timed out) May 26 11:29:19 cmihpx02 vmunix: NFS... (4 Replies)
Discussion started by: rveri
4 Replies

4. Shell Programming and Scripting

awk to find lines containing word that occur multiple times

i have a script that scans a log file every 10 minutes. this script remembers the last line of the log and then uses it to continue monitoring the log when it runs again 10 minutes later. the script searches the log for a string called MaxClients. now, how can i make it so that when the... (7 Replies)
Discussion started by: SkySmart
7 Replies

5. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

6. Shell Programming and Scripting

awk? extract quoted "" strings from multiple lines.

I am trying to extract multiple strings from snmp-mib files like below. ----- $ cat IF-MIB.mib <snip> linkDown NOTIFICATION-TYPE OBJECTS { ifIndex, ifAdminStatus, ifOperStatus } STATUS current DESCRIPTION "A linkDown trap signifies that the SNMP entity, acting in... (5 Replies)
Discussion started by: genzo
5 Replies

7. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

8. Shell Programming and Scripting

AWK Duplicate lines multiple times based on a calculated value

Hi, I'm trying to create an XML sitemap of our dynamic ecommerce sites SEO Friendly URLs and am trying to create the initial page listing. I have a CSV file that looks like the following and need duplicate the lines based on a value which needs calculating. ... (2 Replies)
Discussion started by: jamesfx
2 Replies

9. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

10. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies
Login or Register to Ask a Question