multiline pattern matching

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting multiline pattern matching
# 1  
multiline pattern matching

I have a file of the following from:

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

Afghanistan 800
Albania 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  
I have 'outflanked' the problem but I'd still like to know whether there's a solution for this?
# 3  
if you have Python, here's an alternative solution
data=[i.strip() for i in data]
for n,line in enumerate(data):
    if line.startswith("gdpcapit"):        
        for i in data[o:n]:
            print "%s %d"%(i,int(number))
        print line

# ./
Afghanistan 800
Akrotiri 6000
Albania 6000

# 4  
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
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;

# 5  
thanks for the replies. Pyhton / perl were not allowed though.

Last edited by KarelVH; 05-11-2009 at 05:34 AM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #306
Difficulty: Easy
The HTML4 standard was published in 2014.
True or False?

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||015236433 0233654|122555||023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444||015236433 0233654|122555||023365433... (4 Replies)
Discussion started by: imas
4 Replies

Featured Tech Videos