Sponsored Content
Top Forums Shell Programming and Scripting regular expression grouping across multiple lines Post 302706459 by spacebar on Wednesday 26th of September 2012 08:08:53 PM
Old 09-26-2012
This is a simple script that I believe will do what you want, Try it out:
Code:
$ cat book.sh
line_out="["
while read p1 p2 p3
do
  if [ ${p1:0:1} = "b" -a ${p2:0:1} = "p" ]; then
    line_out="$line_out('$p1', '$p3'), "
  elif [ ${p1:0:1} = "b" -a ${p2:0:1} != "p" ]; then
    line_out="$line_out('$p1', "
  elif [ ${p1:0:1} = "p" ]; then
    line_out="$line_out '$p2'), "
  fi
done <input_file
#line_out="$line_out]"
line_out="${line_out%, }]"
echo $line_out

$ cat input_file
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

$ book.sh
[('book1', '23'), ('book2', '22'), ('book3', '24'), ('book4', '25')]

 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

regular expression grepping lines with VARIOUS number of blanks

Hi, I need a regular expression grepping all lines starting with '*' followed by a VARIOUS number of blanks and then followed by the string 'Runjob=1'. I tried that code, but it doesn't work: grep -i '*'+'Runjob=1' INPUT_FILE >>OUTPUT_FILE Can someone help me? Thanks (8 Replies)
Discussion started by: ABE2202
8 Replies

3. 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

4. Shell Programming and Scripting

Would like to print 3 lines after a regular expression is found in the logfile

I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code: grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done The above code things works fine,but sometimes gives erroneous... (3 Replies)
Discussion started by: joachimshaun
3 Replies

5. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

6. 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

7. 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

8. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

9. 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

10. Shell Programming and Scripting

BASH - Regular Expressions :Looking for one word on multiple lines.

Im looking for a bash solution that will use Regular Expressions (not perl, sed or awk) to check the example data below and then give me a status. which would be just simply Match or Mismatch. SYS PS1 is present. Fan status: Normal Input Voltage status: Normal ... (5 Replies)
Discussion started by: popeye
5 Replies
yaml(n) 							  YAML processing							   yaml(n)

__________________________________________________________________________________________________________________________________________________

NAME
yaml - YAML Format Encoder/Decoder SYNOPSIS
package require Tcl 8.4 package require yaml ?0.3.5? ::yaml::yaml2dict ?options? txt ::yaml::yaml2huddle ?options? txt ::yaml::setOption ?options? ::yaml::dict2yaml dict ?indent? ?wordwrap? ::yaml::list2yaml list ?indent? ?wordwrap? ::yaml::huddle2yaml huddle ?indent? ?wordwrap? _________________________________________________________________ DESCRIPTION
The yaml package provides a simple Tcl-only library for parsing the YAML http://www.yaml.org/ data exchange format as specified in http://www.yaml.org/spec/1.1/. The yaml package returns data as a Tcl dict. Either the dict package or Tcl 8.5 is required for use. COMMANDS
::yaml::yaml2dict ?options? txt ::yaml::yaml2huddle ?options? txt Parse yaml formatted text txt into a Tcl dict/huddle and return the value. -file txt is a filename of YAML-stream. -stream txt is just a YAML-stream. -types list The list is a type list for the yaml-scalar types.(e.g. !!str !!timestamp !!integer !!true ...) -types {timestamp integer null true false} In this case, if a string matched "timestamp", converted to the TCL internal timestamp.(e.g. "2001-12-15T02:59:43.1Z" => 1008385183) -m:true param The param is two elements of list for the value of true, and considered strings. -m:true {1 {true on + yes y}} In this case, the string "yes" found in YAML Stream, automatically converted 1. -m:false param The param is two elements of list for the value of false, and considered strings. -m:false {0 {false off - no n}} -m:null param The param is two elements of list for the value of null, and considered strings. -m:null {"" {null nil "" ~}} -validate Experiment,old: Output stream contains YAML's-tag, each node. % puts [::yaml::load -validate {[aaa, bbb]}] => !!seq {{!!str aaa} {!!str bbb}} ::yaml::setOption ?options? Change implicit options for the library. Now, the params are the same as ::yaml::yaml2dict. Arguments of::yaml::yaml2dict is more priority than this setting. ::yaml::dict2yaml dict ?indent? ?wordwrap? ::yaml::list2yaml list ?indent? ?wordwrap? ::yaml::huddle2yaml huddle ?indent? ?wordwrap? Convert a dict/list/huddle object into YAML stream. indent spaces indent of each block node. currently default is 2. wordwrap word wrap for YAML stream. currently default is 40. EXAMPLES
An example of a yaml stream converted to Tcl. A yaml stream is returned as a single item with multiple elements. { --- !<tag:clarkevans.com,2002:invoice> invoice: 34843 date : 2001-01-23 bill-to: &id001 given : Chris family : Dumars address: lines: | 458 Walkman Dr. Suite #292 city : Royal Oak state : MI postal : 48046 ship-to: *id001 product: - sku : BL394D quantity : 4 description : Basketball price : 450.00 - sku : BL4438H quantity : 1 description : Super Hoop price : 2392.00 tax : 251.42 total: 4443.52 comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338. } => invoice 34843 date 2001-01-23 bill-to {given Chris family Dumars address {lines {458 Walkman Dr. Suite #292 } city {Royal Oak} state MI postal 48046}} ship-to {given Chris family Dumars address {lines {458 Walkman Dr. Suite #292 } city {Royal Oak} state MI postal 48046}} product {{sku BL394D quantity 4 description Basketball price 450.00} {sku BL4438H quantity 1 description {Super Hoop} price 2392.00}} tax 251.42 total 4443.52 comments {Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.} An example of a yaml object converted to Tcl. A yaml object is returned as a multi-element list (a dict). { --- - [name , hr, avg ] - [Mark McGwire, 65, 0.278] - [Sammy Sosa , 63, 0.288] - Mark McGwire: {hr: 65, avg: 0.278} Sammy Sosa: { hr: 63, avg: 0.288} } => {name hr avg} {{Mark McGwire} 65 0.278} {{Sammy Sosa} 63 0.288} {{Mark McGwire} {hr 65 avg 0.278} {Sammy Sosa} {hr 63 avg 0.288}} LIMITATIONS
tag parser not implemented. currentry, tags are merely ignored. Only Anchor => Aliases ordering. back alias-referring is not supported. Too many braces, or too few braces. Not enough character set of line feeds. Please use only " " as line breaks. BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category yaml of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. SEE ALSO
base64, huddle, json KEYWORDS
data exchange, huddle, parsing, text processing, yaml COPYRIGHT
Copyright (c) 2008 KATO Kanryu <kanryu6@users.sourceforge.net> yaml 0.3.5 yaml(n)
All times are GMT -4. The time now is 09:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy