Add newline before another line of occurance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add newline before another line of occurance
# 1  
Old 01-20-2014
Add newline before another line of occurance

Hi ,

I have a file like
Quote:
Cat
Realized
Dog
Realized
Sheep
s
d
f
jk
Realized
Wolf
Realized
Sheep
s
d
Realized
I want to add a new line before "Realized" only when it comes after "Sheep".
There may be any line betwwen "Sheep" and "Realized" other than this two.
Say my new line is "a goat", so the desired result would be
Quote:
Cat
Realized
Dog
Realized
Sheep
s
d
f
jk
a goat
Realized
Wolf
Realized
Sheep
s
d
a goat
Realized
Can any body help me in this?

thanks
# 2  
Old 01-20-2014
Code:
awk '
/Sheep/ {found=1}
found==1 && /Realized/ {print "new line";
found=0}
{print}
' file


Last edited by MadeInGermany; 01-20-2014 at 05:29 AM.. Reason: explicit {print} added
# 3  
Old 01-20-2014
Code:
$ awk '/Sheep/{F=1} /Realized/{if(F){print "a goat"}else{F=""}} {print}' file
Cat
Realized
Dog
Realized
Sheep
s
d
f
jk
a goat
Realized
Wolf
a goat
Realized
Sheep
s
d
a goat
Realized

# 4  
Old 01-20-2014
Hi "MadeInGermany",
Thanks for your reply. But can you please explain a bit. I am very new in this.

Thanks again..

---------- Post updated at 01:08 PM ---------- Previous update was at 01:03 PM ----------

Hi Pamu,

Its printing new line before every "Realized" once it found "Sheep".
But newline "a goat" is not required between "WOLF" and "Realized".

Though thanks for you reply.
# 5  
Old 01-20-2014
# 6  
Old 01-20-2014
An explanation of my previous post follows.
The shell runs the awk command and passes a (multi-line) argument in ticks, and another argument (file). (The ticks are also called single quotes.)
The awk sees two statements in the 1st argument.
awk statements are usually in curly brackets. Here they are prefixed with a condition (implicit if clause).
Multiple statements in a curly bracket block are separated by a semicolon - or are on separate lines.
awk runs the statements for each line of its input file(s).
The logic is simple here:
if Sheep is found in the current input line, store 1 in a variable (count). Note that initially a variable is empty (0 in number context).
If found is equal to 1 (because was set to 1 in this or a previous input line) and Realized is found in the current input line then print the new line; also reset found to 0 so if Realized is met again it won't print another new line.
Then (I just added it to my previous post) print the input line.
# 7  
Old 01-20-2014
Thanks MadeInGermany,

It works fine for me.

thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting an unexpected newline in my while loop line-by-line feed

Hi, I'm trying to get a line returned as is from the below input.csv file in Bash in Linux, and somehow I get an unexpected newline in the middle of my input. Here's a sample line in input.csv $> more input.csv TEST_SYSTEM,DUMMY@GMAIL.COM|JULIA H|BROWN And here's a very basic while loop... (7 Replies)
Discussion started by: ChicagoBlues
7 Replies

2. Shell Programming and Scripting

Replace 3rd occurance of SPACE with newline

I have file with SQL output as 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Expected output : 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Let me know if this can... (9 Replies)
Discussion started by: sameermohite
9 Replies

3. UNIX for Dummies Questions & Answers

Print a newline after first match in line

Hi everyone I have a file where CP occurs both within each line and at the very end: dwer 17 knsdask= * CP hwla 17 h'wopie un CP I would like to separate the line on the first CP to get: dwer 17 knsdask= * CP hwla 17 h'wopie un CP What I have so far is: awk '{for (x=1; x<NF; x++) ... (5 Replies)
Discussion started by: meet77
5 Replies

4. Shell Programming and Scripting

How to find frequent occurance of a word in a line?

File_source.DAT 1|abc|abc|abc|abc|abc 2|abc|abc|efg|efg|def 3|abc|bcd|cde|def|efg 4|abc|abc|abc|def|efg ========================= Please help me to solve this as below using UNIX. ========================= File_output.DAT "1"|"abc" - as... (3 Replies)
Discussion started by: scpyraj
3 Replies

5. Shell Programming and Scripting

Multi line document to single lines based on occurance of string

Hi Guys, I am new to awk and sed, i am working multiline document, i want to make make that document into SINGLE lines based on occurace of string "dwh". here's the sample of my problem.. dwh123 2563 4562 4236 1236 78956 12394 4552 dwh192 2656 46536 231326 65652 6565 23262 16625623... (5 Replies)
Discussion started by: victor369
5 Replies

6. Shell Programming and Scripting

How to separate a line with or without using newline command?

Hi, I am using Putty for unix shell scripting. I need some suggestions in splitting the 'output line' into two separate lines. Originally I am getting the input from another text file A. And when looking at the content in the text file A, the lines are separated in the way I want. After that I... (10 Replies)
Discussion started by: snr100
10 Replies

7. Shell Programming and Scripting

use regexp to insert newline within a line

I have successfully used regexp and sed to insert a newline before or after a line containing a matched pattern /WORD/. However, I want to insert a newline immediately following /WORD/ and not after the -line- containing the pattern matched. I can match a pattern, but it is matched via a wild card... (2 Replies)
Discussion started by: kpeirce
2 Replies

8. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

9. Shell Programming and Scripting

How to replace specific text line out of multiple occurance

Hi I would like to replace specific line eg ExitAction = NONE to ExitAction = FALSE under only TASK sipsiproc and other ExitAction = NONE will remain as usual in the file(shell script) The file contains: TASK rgcdproc { CommandLine = $SSHOME/bin/rgcd.exe NewConsole... (5 Replies)
Discussion started by: madhusmita
5 Replies

10. Shell Programming and Scripting

split line when found newline

Hi , I have below problem and needs your advice, i have a file a.txt as below 12|romario|ronaldo|robert 23|aaa|bbb|haaa 000000002 There is no issues when i open this file in window,it will sows as it is. in Unix: i want the last record into seperate file. So i used $ tail... (2 Replies)
Discussion started by: HAA
2 Replies
Login or Register to Ask a Question