Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-22-2012, 03:09 PM   #1
LQ Newbie
Registered: Jan 2012
Posts: 6

Rep: Reputation: Disabled
sed or awk conditional string replacement

I have the following string. If the string starts with
EXEC SQL INCLUDE then I need to replace the entire string with COPY filename.cpy and this should start in col 10. Is there a way to do this with sed or awk?


After replacement inc col 10:
COPY filename.cpy
Old 01-22-2012, 03:13 PM   #2
Ser Olmy
Senior Member
Registered: Jan 2012
Distribution: Slackware
Posts: 2,468

Rep: Reputation: Disabled
This ought to do it:
sed "s/EXEC\ SQL\ INCLUDE\ \(.*\)\ END-EXEC/COPY \1.cpy/"
What do you mean by "column 10"? How are these columns separated?

Last edited by Ser Olmy; 01-22-2012 at 03:15 PM.
Old 01-23-2012, 12:31 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
It would seem difficult to assist when you refer to column 10 when there are only 5 columns (assuming space as delimiter).
Are you able to clarify?
Old 01-23-2012, 01:14 PM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Hitting the "quote" button lets us see the actual formatting the OP wants, and by "column" he means the text should start at the tenth position on the line. So just insert the required number of spaces in front of the replacement text string.

Also, you can avoid all that hard-to-read "picket fence" escaping by adding the -r option.

sed -r 's/EXEC SQL INCLUDE (.*) END-EXEC/          COPY \1.cpy/'

And for further study...

Here are a few useful sed references.

Here are a few useful awk references:


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
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 08:59 AM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 08:01 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 09:04 PM
Replacement with sed or awk DeepSeaNautilus Programming 4 08-05-2008 12:17 PM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 12:53 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:12 PM.

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