Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Finding lines with a regular expression, replacing them with blank lines Post 302644675 by Golpette on Tuesday 22nd of May 2012 09:01:15 AM
Old 05-22-2012
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:

Code:
sed -e 's/REGEXP[^ ]*/MARKER/g' -e 's/[^ ]*MARKER//g' in.txt > out.txt

So I find a REGEXP, replace that and the rest of the line with "MARKER", then I replace the start of the line, including "MARKER" with nothing, thus leaving behind a blank entry...

I think this has worked... Better method please?!
 

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

replacing blank lines

Hi i am trying to replace blank lines with a number 0. I tried the following code awk '{print NF ? $0: blankrow}' blankrow = "0" file1.prg>file2.prg however i get the following error: fatal: cannot open file `blankrow' for reading (No such file or directory) file example: 1 2 3 5 6... (11 Replies)
Discussion started by: rockiefx
11 Replies

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

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

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

6. Shell Programming and Scripting

Help in replacing two blank lines with two diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. Sed "/data/{G;G;}/" filename. In the file, after data tag, two lines got inserted blank lines.. Now... (1 Reply)
Discussion started by: arjun_arippa
1 Replies

7. Shell Programming and Scripting

Help in replacing two blank lines with two lines of diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. sed "/data/{G;G;}/" filename . In the file, after data tag, two lines got inserted blank lines..... (4 Replies)
Discussion started by: arjun_arippa
4 Replies

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

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

10. 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
Boulder::Unigene(3pm)					User Contributed Perl Documentation				     Boulder::Unigene(3pm)

NAME
Boulder::Unigene - Fetch Unigene data records as parsed Boulder Stones SYNOPSIS
# parse a file of Unigene records $ug = new Boulder::Unigene(-accessor=>'File', -param => '/data/unigene/Hs.dat'); while (my $s = $ug->get) { print $s->Identifier; print $s->Gene; } # parse flatfile records yourself open (UG,"/data/unigene/Hs.dat"); local $/ = "*RECORD*"; while (<UG>) { my $s = Boulder::Unigene->parse($_); # etc. } DESCRIPTION
Boulder::Unigene provides retrieval and parsing services for UNIGENE records Boulder::Unigene provides retrieval and parsing services for NCBI Unigene records. It returns Unigene entries in Stone format, allowing easy access to the various fields and values. Boulder::Unigene is a descendent of Boulder::Stream, and provides a stream-like interface to a series of Stone objects. Access to Unigene is provided by one accessors, which give access to local Unigene database. When you create a new Boulder::Unigene stream, you provide the accessors, along with accessor-specific parameters that control what entries to fetch. The accessors is: File This provides access to local Unigene entries by reading from a flat file (typically Hs.dat file downloadable from NCBI's Ftp site). The stream will return a Stone corresponding to each of the entries in the file, starting from the top of the file and working downward. The parameter is the path to the local file. It is also possible to parse a single Unigene entry from a text string stored in a scalar variable, returning a Stone object. Boulder::Unigene methods This section lists the public methods that the Boulder::Unigene class makes available. new() # Local fetch via File $ug=new Boulder::Unigene(-accessor => 'File', -param => '/data/unigene/Hs.dat'); The new() method creates a new Boulder::Unigene stream on the accessor provided. The only possible accessors is File. If successful, the method returns the stream object. Otherwise it returns undef. new() takes the following arguments: -accessor Name of the accessor to use -param Parameters to pass to the accessor Specify the accessor to use with the -accessor argument. If not specified, it defaults to File. -param is an accessor-specific argument. The possibilities is: For File, the -param argument must point to a string-valued scalar, which will be interpreted as the path to the file to read Unigene entries from. get() The get() method is inherited from Boulder::Stream, and simply returns the next parsed Unigene Stone, or undef if there is nothing more to fetch. It has the same semantics as the parent class, including the ability to restrict access to certain top-level tags. put() The put() method is inherited from the parent Boulder::Stream class, and will write the passed Stone to standard output in Boulder format. This means that it is currently not possible to write a Boulder::Unigene object back into Unigene flatfile form. OUTPUT TAGS
The tags returned by the parsing operation are taken from the names shown in the Flat file Hs.dat since no better description of them is provided yet by the database source producer. Top-Level Tags These are tags that appear at the top level of the parsed Unigene entry. Identifier The Unigene identifier of this entry. Identifier is a single-value tag. Example: my $identifierNo = $s->Identifier; Title The Unigene title for this entry. Example: my $titledef=$s->Title; Gene The Gene associated with this Unigene entry Example: my $thegene=$s->Gene; Cytoband The cytological band position of this entry Example: my $thecytoband=$s->Cytoband; Counts The number of EST in this record Example: my $thecounts=$s->Counts; LocusLink The id of the LocusLink entry associated with this record Example: my $thelocuslink=$s->LocusLink; Chromosome This field contains a list, of the chromosomes numbers in which this entry has been linked Example: my @theChromosome=$s->Chromosome; STS Multiple records in the form ^STS ACC=XXXXXX NAME=YYYYYY ACC NAME TXMAP Multiple records in the form ^TXMAP XXXXXXX; MARKER=YYYYY; RHPANEL=ZZZZ The TXMAP tag points to a Stone record that contains multiple subtags. Each subtag is the name of a feature which points, in turn, to a Stone that describes the feature's location and other attributes. Each feature will contain one or more of the following subtags: MARKER RHPANEL PROTSIM Multiple records in the form ^PROTSIM ORG=XXX; PROTID=DBID:YYY; PCT=ZZZ; ALN=QQQQ Where DBID is PID for indicate presence of GenPept identifier, SP to indicate SWISSPROT identifier, PIR to indicate PIR identifier, PRF to indicate ??? ORG PROTID PCT ALN SEQUENCE Multiple records in the form ^SEQUENCE ACC=XXX; NID=YYYY; PID = CLONE= END= LID= ACC NID PID CLONE END LID SEE ALSO
Boulder, Boulder::Blast, Boulder::Genbank AUTHOR
Lincoln Stein <lstein@cshl.org>. Luca I.G. Toldo <luca.toldo@merck.de> Copyright (c) 1997 Lincoln D. Stein Copyright (c) 1999 Luca I.G. Toldo This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See DISCLAIMER.txt for disclaimers of warranty. perl v5.10.1 2000-01-18 Boulder::Unigene(3pm)
All times are GMT -4. The time now is 05:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy