multiline pattern matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting multiline pattern matching
# 1  
Old 05-10-2009
multiline pattern matching

Hi,
I have a file of the following from:
Afghanistan
gdpcapit|800
Akrotiri
Albania
gdpcapit|6000


now I want have the gdpcapit value next to the country when there is one like this:

Afghanistan 800
gdpcapit|800
Akrotiri
Albania 6000
gdpcapit|6000


How do I do this? I've been searching for this all day...

On the machine i'm working on there is no grep that supports -B or -A... there should be a gawk or gsed though

thanks in advance
# 2  
Old 05-10-2009
I have 'outflanked' the problem but I'd still like to know whether there's a solution for this?
# 3  
Old 05-10-2009
if you have Python, here's an alternative solution
Code:
o=0
data=open("file").readlines()
data=[i.strip() for i in data]
for n,line in enumerate(data):
    line=line.strip()
    if line.startswith("gdpcapit"):        
        number=line.split("|")[-1]
        for i in data[o:n]:
            print "%s %d"%(i,int(number))
        print line
        o=n+1

output
Code:
# ./test.py
Afghanistan 800
gdpcapit|800
Akrotiri 6000
Albania 6000
gdpcapit|6000

# 4  
Old 05-11-2009
if your file is in small size, below perl code should work for you.

if it is a huge size file, may need to resort to other solutionSmilie
Code:
my @tmp=<DATA>;
map {s/\n//} @tmp;
my @arr=reverse @tmp;
for (my $i=1;$i<=$#arr;$i++){
	if($arr[$i-1] =~ /gdpcapit\|/ && $arr[$i] !~ /\|/){
		my @t=split("[|]",$arr[$i-1]);
		$arr[$i].=" ".$t[1]
	}
}
map {print $_,"\n"} reverse @arr;
__END__
Afghanistan
gdpcapit|800 
Akrotiri
Albania
gdpcapit|6000

# 5  
Old 05-11-2009
thanks for the replies. Pyhton / perl were not allowed though.

Last edited by KarelVH; 05-11-2009 at 05:34 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

2. Shell Programming and Scripting

Using sed to pattern match within a particular multiline block and take action

Hi all, This is my first post, so please go easy if I broke some rules. Not accustomed to posting in forums... :) I'm looking for help on pattern matching within a multiline block and looking to highlight blocks/block-ids that do NOT contain a particular pattern. For example an input file... (5 Replies)
Discussion started by: tirodad
5 Replies

3. UNIX for Dummies Questions & Answers

sed multiline pattern match

How can I write a script that takes a cisco config file and outputs every occurrence of two, or more, pattern matches through the whole config file? For example, out of a config file, i want to print out every line with interface, description and ip address through the whole file, and disregard... (3 Replies)
Discussion started by: knownasthatguy
3 Replies

4. Shell Programming and Scripting

awk multiline matching

I have a file that looks something like this with lots of text before and after. Distance method: Sum of squared size difference (RST) </data> <pairwiseDifferenceMatrix time="02/08/11 at 13:08:27"> 1 2 1 448.82151 507.94231 2 ... (7 Replies)
Discussion started by: mgray
7 Replies

5. Shell Programming and Scripting

Regarding multiline record searching with specific pattern

Dear Experts, I need to extract specific records from one file which has multiline records. Input file pattern is: ============ aaaaaaaa bbbbbbbb asdf 1234 cccccccc dddddddd ============ aaaaaaaa bbbbbbbb qwer 2345 cccccccc dddddddd (7 Replies)
Discussion started by: dhiraj4mann
7 Replies

6. Shell Programming and Scripting

Multiline parenthesis matching, with e.g. SED script, in LaTeX doc

In a LaTeX manuscript, I need to replace many occurrences of \emph{some string} with some string, i.e. whatever string is inside. The string inside often may extend over several lines, and there may be other occurences of curly brackets inside it. So for example \emph{this \it{is} a... (5 Replies)
Discussion started by: sune
5 Replies

7. Shell Programming and Scripting

Multiline pattern search using sed or awk

Hi friends, Could you please help me to resolve the below issue. Input file :- <Node> <username>abc</username> <password>ABC</password> <Node> <Node> <username>xyz</username> <password>XYZ</password> <Node> <Node> <username>mnp</username> ... (3 Replies)
Discussion started by: haiksuresh
3 Replies

8. Shell Programming and Scripting

Awk match a multiline pattern

Hello! i wanna match in a config file, one text with more than one lines, something like this: CACHE_SIZE{ 10000 M } I have problems with the ends of line, i think that i can match the end of the line with \n, but i can't get it Someone can help me with the regular expression? ... (18 Replies)
Discussion started by: claw82
18 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question