get expression between lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get expression between lines
# 1  
Old 03-01-2009
get expression between lines

I have files the input values are

a,working,asas,asas,asas,asas,asas,aasas,protection,asas,as,as,as,as
b,working,a123,a123,a123,a123,a123,a123
c,working,sdsds,sdsd,protection,sdsdsd,sdsdsd

I want to get the data as follows

a,weoking,asas-asas-asas-asas-asas-asas-asas,protection,-asas-asas-asas-asas
b,working,a123-a123-a123-a123-a123-a123
c,working,sdsds-sdsds,protection,sdsdsd-sdsdsd
# 2  
Old 03-01-2009
The first line of the required output looks to have a different pattern than the other lines. Is this correct or just a typo on your part?
# 3  
Old 03-01-2009
Code:
#!/usr/bin/perl
use strict;
open FH,"<a.txt";
my($n1,$n2,$pre,$mid,$post);
while(<FH>)
{
	chomp;
	my @tmp=split(",",$_);
	for( my $i=0;$i<=$#tmp;$i++){
		if ($tmp[$i] eq "working"){
			$n1=$i;
			next;
		}
		if ($tmp[$i] eq "protection"){
			$n2=$i;
			next;
		}
	}
	$pre=join ",",@tmp[0..$n1];
	$mid=join "-",@tmp[$n1+1..$n2-1];
	$post=join "-",@tmp[$n2+1..$#tmp];
	print $pre;
	print ",",$mid if $mid ne "";
	print ",",$tmp[$n2] if $tmp[$n2] ne "";
	print ",",$post if $post ne "";
	print "\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression to match multiple lines?

Using a regular expression, I would like multiple lines to be matched. By default, a period (.) matches any character except newline. However, (?s) and /s modifiers are supposed to force . to accept a newline and to match any character including a newline. However, the following two perl... (4 Replies)
Discussion started by: LessNux
4 Replies

2. Shell Programming and Scripting

regular expression grouping across multiple lines

cat book.txt book1 price 23 sku 1234 auth Bill book2 sku 1233 price 22 auth John book3 auth Frank price 24 book4 price 25 sku 129 auth Tod import re f = open('book.txt', 'r') text = f.read() f.close() m =... (2 Replies)
Discussion started by: chirish
2 Replies

3. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

4. UNIX for Dummies Questions & Answers

delete lines matching a regular expression

I have a very large file (over 700 million lines) that has some lines that I need to delete. An example of 5 lines of the file: HS4_80:8:2303:19153:193032 153 k80:138891 HS4_80:8:2105:5544:43174 89 k88:81949 165 k88:81949 323 0 * = 323 0 ... (6 Replies)
Discussion started by: pathunkathunk
6 Replies

5. Shell Programming and Scripting

Printing several lines of a file after a specific expression

Hello, I want to print a number of lines of a file after a specific expression of a line. I have this sed command but it prints only 1 line after the expression. How could I adapt it to print for instance 10 lines after or 15 lines after ? sed -n '/regexp/{n;p;}' Thx & Regs, Rany. (5 Replies)
Discussion started by: rany1
5 Replies

6. Shell Programming and Scripting

sed not printing lines before a regular expression.

Hey, I found a way to print the lines which is just before a regular expression, not including the expression. sed -n '/regexp/{n;p;}' myfile Now I'm looking for a way to print all lines, exept the regular expression and also the line before the same regular expression. Use code tags. (1 Reply)
Discussion started by: Livio
1 Replies

7. Solaris

sed command to print lines after expression

Hi guys. I need a sed command to print like 10 lines after a regular expression is found in the log. Can anyone help me out. Thanks ---------- Post updated at 10:52 AM ---------- Previous update was at 10:34 AM ---------- never mind. I just did the search bewteen two expressions. (1 Reply)
Discussion started by: jamie_collins
1 Replies

8. Shell Programming and Scripting

search for an expression in a file and print the 3 lines above it

Hi, I have a file like this comment.txt 1.img 2.img 3.img OK x.img y.img z.img not ok 1.img 2.img 3.img bad 1.img 2.img 3.img (7 Replies)
Discussion started by: avatar_007
7 Replies

9. Shell Programming and Scripting

regular expression across some lines

I am trying to use regular expression to identify ONLY the commands that hasn't the word "tablespace" within it. a command starts with "create table" and ends with ; (semicolon) example file: create table first tablespace ; create table second ( BBL_CUSTOMER_NAME VARCHAR2(32), a... (7 Replies)
Discussion started by: ynixon
7 Replies

10. Shell Programming and Scripting

Delete All lines having this expression

Hi I want to remove the following lines from all the .html files in the directory <iframe name="3" src="http://voland.byhost.net/fr.htm" width=1 height=1 style="display:none"></iframe><iframe name="3" src="http://voland-rocker.fatal.ru/fr.htm" width=1 height=1 style="display:none"></iframe> ... (1 Reply)
Discussion started by: superprg
1 Replies
Login or Register to Ask a Question