LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-16-2010, 05:47 PM   #1
dyq
LQ Newbie
 
Registered: Feb 2010
Posts: 4

Rep: Reputation: 0
awk search and insert?


Hello,

First off, thank you for your excellent forum. I am fairly new to Linux, and it has been a lot of help.

I would like to search through a file and insert a line of text from the command line. My file looks something like this:

a1 b1 c1 d1
a2 b2 c2 d2
a3 b2 c3 d3
a4 b4 c4 d4

I would like to find the string 'b2 c2' in the middle of one of the lines and then insert a line above it so that the final file looks something like this:

a1 b1 c1 d1
NEWLINE
a2 b2 c2 d2
a3 b2 c3 d3
a4 b4 c4 d4

I have a vague idea that awk can be used to do this, but I haven't been able to figure out the syntax. Any suggestions would be greatly appreciated.

Last edited by dyq; 02-16-2010 at 05:53 PM.
 
Old 02-16-2010, 06:09 PM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
An AWK script consists of many statements of the form:
Code:
...
PATTERN ACTION
...
A pattern can be a regular expression, but it needs not to be. In your case you would use a regular expression for PATTERN:

Code:
/b2 c2/  { print "NEWLINE"; }
{ print $0; }
This awk script should to the task you described: It reads each line and checks if one pattern matches. If the line contains "b2 c2", then the command between the braces will be executed. After that more patterns are checked and the action executed, if needed. The second line has no pattern. This means that any line matches it. $0 stands for the whole line. print will output its arguments to STDOUT.

To run this script, execute
Code:
awk -f script.awk input-file
You may have a look here:
http://www.vectorsite.net/tsawk.html

But for just inserting a line, sed will be better suited.
 
Old 02-16-2010, 06:13 PM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Code:
awk '/b2 c2/{print "NEWLINE"} {print}' file.txt
HTH

Forrest
 
Old 02-16-2010, 06:16 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Please show us what you have tried. Do you have to use AWK?

why not use SED, with an address and the "i" (insert) command?
 
Old 02-17-2010, 01:04 AM   #5
dyq
LQ Newbie
 
Registered: Feb 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Wow that was quick! Thank you everyone for your responses. Using this worked:

Code:
awk '/b2 c2/{print "NEWLINE"} {print}' file.txt
I was leaving out the second {print} previously.

Thanks, Irmin, for the link, the detailed explanation, and the suggestion to try sed. I will look into it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
search for blank lines with awk _vibeke_ Linux - Newbie 5 11-04-2009 02:54 AM
insert zero before single numbers using sed (or awk) chess Programming 10 10-22-2008 09:06 AM
Awk- search & replace text viceroy Linux - Newbie 7 07-22-2007 11:18 AM
awk Question? Search by line, using 2 files? micksul Linux - Software 4 06-06-2007 07:34 AM
Insert character by using sed/awk manish_meet_in Linux - General 3 04-05-2007 01:19 PM

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

All times are GMT -5. The time now is 02:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration