any savant ? using AWK/SED to remove newline character between two strings : conditional removal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting any savant ? using AWK/SED to remove newline character between two strings : conditional removal
# 1  
Old 03-02-2011
Bug any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex )
---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1
-- After the 1st occurrence of 1 string1 till the 1st occurrence of string2 not counting the latter.

e.g.
Code:
Begin Transaction ;
She sells
sea shells 
by 
the 
sea shore
End Transaction ;

Begin Transaction ;
For if she sells sea-shells on the sea-shore
 Then I'm sure she sells sea-shore shells.
End transaction ;


Begin Transaction ;
The shells sh
e sells a
re sea-sh
ells, I'm s
ure.

/* Notice How I made the above English incoherent by putting newline breaking words, those newline need to go */
End Transaction ;


Code:
replace function () 
Blah

- How do I go about knocking of the newline char between Last Begin and End transaction pair
Program Thought : Go find the 3rd Begin transaction statement and knock off the newline until the 1st end transaction statement


Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 03-02-2011 at 03:50 AM.. Reason: code tags
# 2  
Old 03-02-2011
Try:
Code:
awk -vRS= 'NR==3{gsub("\n","")}1' file

# 3  
Old 03-02-2011
Two approaches:

Code:
awk '/^End [Tt]ransaction/&&g==3{print "";g++}g==3{printf $0; next}/^Begin Transaction/{g++} 1' infile

Code:
awk 'NR==3{f=split($0,A,"\n");print A[1];for(i=2;i<f;)printf A[i++];$0="\n"A[f]} $0=$0"\n"' RS= infile

Both the 2nd solution here and bartus11's post above rely on a blank line after the End line. Unlike Bartus11's solution the 2nd solution here ensures the Beg and End lines aren't joined up with the rest of the text, and restores the blank line after then End line.

Last edited by Chubler_XL; 03-02-2011 at 11:23 PM..
# 4  
Old 03-02-2011
Question

That was very helpful except for the fact that it does not 'think' like this .
What I am trying to do is getting rid of the newline characters - that erroneously break the word and make things incoherent.
LOGIC:


-- Go Find a String1
---Look for the nth instance of String1
-- From the nth instance onwards , excluding string1 : `do this stuff`
-- Keep doing till you come to the 'm th' instance of string 2 , excluding String2.
The above was a brilliant idea , except for the fact that I cannot gurantee that there is a paragraph in every file , that can be used as line separator
So your looking at something like this :



Code:
Begin Transaction ;
She sells
sea shells
by
the
sea shore
End Transaction ;
Begin Transaction ;
For if she sells sea-shells on the sea-shore
 Then I'm sure she sells sea-shore shells.
End transaction ;
Begin Transaction ;
The shells sh
e sells a
re sea-sh
ells, I'm s
ure.
replace function ()
Blah

Output should be :

Code:
Begin Transaction ;
She sells
sea shells
by
the
sea shore
End Transaction ;
Begin Transaction ;
For if she sells sea-shells on the sea-shore
 Then I'm sure she sells sea-shore shells.
End transaction ;
Begin Transaction ;
The shells she sells are sea-shells, I'm sure.
replace function ()
Blah

.The above AWK gives this o/p

Code:
Begin Transaction ;
She sells
sea shells
by
the
sea shore
End Transaction ;
Begin Transaction ;
For if she sells sea-shells on the sea-shore
 Then I'm sure she sells sea-shore shells.
End transaction ;
/* there is no newline char after the begin transaction below. It shd be there * /
Begin Transaction ;The shells she sells are sea-shells, I'm sure.
replace function ()
Blah

THANKS for your help
# 5  
Old 03-03-2011
My solution #1 (no dependance on blank lines) produces the following output for your 2nd example:

Code:
Begin Transaction ;
She sells
sea shells
by
the
sea shore
End Transaction ;
Begin Transaction ;
For if she sells sea-shells on the sea-shore
 Then I'm sure she sells sea-shore shells.
End transaction ;
Begin Transaction ;
The shells she sells are sea-shells, I'm sure.replace function ()Blah

How is the program to know to stop joining lines at replace function () ? Should it to stop joining once a fullstop char (".") is encountered?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

How to remove certain character strings with awk?

Hi all, I need to remove DBPATH= and /db from the string below using awk (or sed, as it also exists on the machine). Input: DBPATH=/some/path/database/db Desired output: /some/path/database Thank you! (8 Replies)
Discussion started by: ejianu
8 Replies

3. UNIX for Dummies Questions & Answers

Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how. I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make... (17 Replies)
Discussion started by: Twinklefingers
17 Replies

4. Shell Programming and Scripting

Remove last newline character..

Hi all.. I have a text file which looks like below: abcd efgh ijkl (blank space) I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file. I have tried options like... (14 Replies)
Discussion started by: Sathya83aa
14 Replies

5. Shell Programming and Scripting

Want to remove / and character using awk or sed

Below i am trying to remove "/" and "r" from the output, so i need output as: hdiskpower3 hdisk0 hdisk1 #inq | grep 5773 | awk '{print $1}' | sed 's/dev//g' | awk -F"/" '{$1=$1}1' .....................................................//rhdiskpower0 //rhdiskpower1 //rhdiskpower2... (3 Replies)
Discussion started by: aix_admin_007
3 Replies

6. Shell Programming and Scripting

awk or sed script to remove strings

Below am trying to separate FA-7A:1, In output file it should display 7A 1 Command am using Gives same output as below format: 22B7 10000000c9720873 0 22B7 10000000c95d5d8b 0 22BB 10000000c97843a2 0 22BB 10000000c975adbd 0 Not showing FA ports as required format... (5 Replies)
Discussion started by: aix_admin_007
5 Replies

7. Shell Programming and Scripting

Remove newline character between two delimiters

hi i am having delimited .dat file having content like below. test.dat(5 line of records) ====== PT2~Stag~Pt2 Stag Test. Updated~PT2 S T~Area~~UNCEF R20~~2012-05-24 ~2014-05-24~~ PT2~Stag y~Pt2 Stag Test. Updated~PT2 S T~Area~METR~~~2012-05-24~2014-05-24~~test PT2~Pt2 Stag Test~~PT2 S... (4 Replies)
Discussion started by: sushine11
4 Replies

8. Shell Programming and Scripting

sed help with character removal

Hello I've got a string of text with a number in pence, e.g. 0.52p, I need to remove the 'p' so that it just reads 0.52 without of course removing all the other 'p' characters. Many thanks (1 Reply)
Discussion started by: mrpugster
1 Replies

9. Shell Programming and Scripting

sed/awk remove newline

Hi, I have input file contains sql queries i need to eliminate newlines from it. when i open it vi text editor and runs :%s/'\n/'/g it provides required result. but when i run sed command from shell prompt it doesn't impact outfile is still same as inputfile. shell] sed -e... (6 Replies)
Discussion started by: mirfan
6 Replies

10. Shell Programming and Scripting

Special Character SED/AWK removal

I have a script that produces an output containing '/.ssh'. I am trying to find a way of parsing only this data from a single line, without removing any other special characters contained within the output as a result of the parse. Any help would be appreciated (6 Replies)
Discussion started by: Raggedranger333
6 Replies
Login or Register to Ask a Question