LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-18-2013, 01:35 AM   #1
zabi
LQ Newbie
 
Registered: Sep 2013
Posts: 9

Rep: Reputation: Disabled
Need to insert a newline after 3rd line of grep pattern search


I have a file e.g

Fedora
Linux
MAC
Windows

it should grep 3 lines
e.g grep -A2 Fedora file

The o/p is

Fedora
Linux
MAC

And I want to insert a new line after 3rd line of last grep so o/p should be:

Fedora
Linux
MAC
NEWLINE
Windows

How to do it with[sed,awk,grep]??
 
Old 12-18-2013, 02:13 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by zabi View Post
I want to insert a new line after 3rd line of last grep so o/p should be:

Fedora
Linux
MAC
NEWLINE
Windows

How to do it with[sed,awk,grep]??
This awk script does it:
Code:
/Fedora/        { F=1 }
F>3             { printf "\n"; F=0 }
F               { F++ }
                { print }
When the line contains the string Fedora, a variable F is initialised to 1. We use this variable to count the lines after "Fedora".
When F is greater than 3, it's time to print a newline. Our job is done for now; reset F.
When F is non-zero, increment it.
And finally, print the input line.

I am sure it can be done in a much more elegant way.
 
Old 12-18-2013, 02:34 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Code:
awk '/Fedora/{x=NR+3}NR==x{print ""}1' file
 
1 members found this post helpful.
Old 12-18-2013, 03:04 AM   #4
zabi
LQ Newbie
 
Registered: Sep 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Perfect it worked!!
Thanks!!
 
Old 12-18-2013, 03:08 AM   #5
zabi
LQ Newbie
 
Registered: Sep 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
i want the file to be edited at the same time,Currently it looks like this:

[XXXXXX # awk '/Fedora/{x=NR+3}NR==x{print "DON"}1' sdf
Fedora
Linux
MAC
DON
Windows
[XXXXXX # cat sdf
Fedora
Linux
MAC
Windows
[XXXXX #
 
Old 12-18-2013, 03:26 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Code:
 
# cp sdf sdf.backup-in-case-something-goes-wrong
# awk '/Fedora/{x=NR+3}NR==x{print "DON"}1' sdf >sdf.temp
# mv sdf.temp sdf
 
Old 12-18-2013, 03:53 AM   #7
zabi
LQ Newbie
 
Registered: Sep 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
So there is no other way like sed -i works....
Anyways... Thanks a lot!
 
Old 12-18-2013, 04:47 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Using sed:
Code:
$ cat input
Fedora
Linux
MAC
Windows
$ sed -i.bak '/Fedora/ {N;N;s/$/\nDON/}' input
$ cat input
Fedora
Linux
MAC
DON
Windows
the .bak following the -i option makes a backup, just in case.
 
Old 12-18-2013, 05:06 AM   #9
zabi
LQ Newbie
 
Registered: Sep 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Great!!
Thanks a lot!!
 
Old 12-18-2013, 11:01 PM   #10
zabi
LQ Newbie
 
Registered: Sep 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
resolved

Last edited by zabi; 12-19-2013 at 12:22 AM. Reason: got it resolved with double quotes
 
Old 12-24-2013, 11:04 AM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by berndbausch View Post
Code:
 
awk '/Fedora/{x=NR+3}NR==x{print "DON"}1' sdf >sdf.temp
With this InFile ...
Code:
Fedora
Linux
MAC
Windows
Fedora
Derby
Stetson
Bowler
... et cetera ...
... this code ...
Code:
awk '/Fedora/{x=NR+3}NR==x{print "DON"}1' $InFile >$OutFile
... produced this OutFile ...
Code:
Fedora
Linux
MAC
DON
Windows
Fedora
Derby
Stetson
DON
Bowler
... et cetera ...
Now, let's allow the line to be inserted to be defined externally.
With the same InFile, this code ...
Code:
new="DON"
awk -v new=$new '/Fedora/{x=NR+3}NR==x{print new}1' $InFile >$OutFile
... produced the same OutFile. So far, so good.

Now, let's change the line to be inserted, making it contain blanks.
With the same InFile, this code ...
Code:
new="D O N"
awk -v new=$new '/Fedora/{x=NR+3}NR==x{print new}1' $InFile >$OutFile
... produced an error message ...
Code:
awk: cmd. line:1: fatal: cannot open file `N' for reading (No such file or directory)
How should this be coded?

Daniel B. Martin
 
Old 12-24-2013, 11:32 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@danielbmartin: You need to use quotes in the awk variable assignment part:
Code:
$ new="D O N"
$ awk -v new="$new" '/Fedora/{x=NR+3}NR==x{print new}1' infile 
Fedora
Linux
MAC
D O N
Windows
Fedora
Derby
Stetson
D O N
Bowler
 
1 members found this post helpful.
Old 12-24-2013, 12:39 PM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by druuna View Post
@danielbmartin: You need to use quotes in the awk variable assignment part ...
That does it. Thank you!

Daniel B. Martin
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
multiple pattern search in a single file line by line saheervc Linux - Newbie 2 09-01-2010 11:45 PM
[SOLVED] Insert line of text prior to pattern only once grail Programming 5 06-29-2010 11:10 AM
[SOLVED] how to make grep to search a pattern in only specific file type mq15 Linux - Newbie 7 03-07-2010 09:41 AM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration