LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-22-2012, 02:09 PM   #1
tsdjim
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?

String:
EXEC SQL INCLUDE filename END-EXEC

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

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 02:15 PM.
 
Old 01-22-2012, 11:31 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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, 12:14 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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.

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

And for further study...

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/
 
  


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


All times are GMT -5. The time now is 02:59 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