LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2011, 06:50 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
conditional substitution


In a CDL formatted netlist we have some lines that look like this;

Quote:
Xh2l<26>.lvl_shft_h2l lvl_shft_h2l $PINS out=out_lv<26> in=in_hv<26> gnd=gnd
and other lines that look like this;

Quote:
Xlevel_shift_l2h level_shift_l2h_2 $PINS in_lv<85>=l2h_in<85>
In the former I want to replace the first occurrence only of "<", ">", or "." with "_". Any occurrence of these symbols after a space need to be left as is.

The second example shows that sometimes there is no occurrence of the symbols prior to a space. In those cases the line is left completely unchanged.

If I were using awk with the default delimiter I would only do a substitutions on the first field. Sounds simple but I don't know how to substitute in awk, I always use sed. However, in sed I have no idea how to substitute characters only when they occur before a space.

Hope this is clear.
 
Old 08-17-2011, 07:04 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Your wording is a little vague. Do you mean you want to replace all instances of all three [<>.] characters, but only in the first field? Or you only want to replace the first instance of each character in the line, as long as it's in the first field? Or only the first instance of any of the three? Or what?

I'm going to assume the first, in which case simply use awk's gsub function.

Code:
awk 'gsub( /[<>.]/ , "_"  ,$1 ) { print }' file
http://www.gnu.org/software/gawk/man...Functions.html

Also, please use [code][/code] tags around your code, to preserve formatting and to improve readability. Do not use quote tags, as they don't preserve whitespace.
 
Old 08-18-2011, 12:28 AM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by David the H. View Post
Your wording is a little vague. Do you mean you want to replace all instances of all three [<>.] characters, but only in the first field? Or you only want to replace the first instance of each character in the line, as long as it's in the first field? Or only the first instance of any of the three? Or what?

I'm going to assume the first, in which case simply use awk's gsub function.

Code:
awk 'gsub( /[<>.]/ , "_"  ,$1 ) { print }' file
http://www.gnu.org/software/gawk/man...Functions.html

Also, please use [code][/code] tags around your code, to preserve formatting and to improve readability. Do not use quote tags, as they don't preserve whitespace.
Your assumption was correct. I'll try this in the morning when I get to work.

Thx...again.
 
Old 08-18-2011, 10:54 AM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by David the H. View Post
Your wording is a little vague. Do you mean you want to replace all instances of all three [<>.] characters, but only in the first field? Or you only want to replace the first instance of each character in the line, as long as it's in the first field? Or only the first instance of any of the three? Or what?

I'm going to assume the first, in which case simply use awk's gsub function.

Code:
awk 'gsub( /[<>.]/ , "_"  ,$1 ) { print }' file
http://www.gnu.org/software/gawk/man...Functions.html

Also, please use [code][/code] tags around your code, to preserve formatting and to improve readability. Do not use quote tags, as they don't preserve whitespace.
This was almost perfect. The only problem was that it does exactly what I said I wanted. Unfortunately there were a few cases where I did not want a "." changed. CDL has keywords that alway have a leading ".", e.g. .ENDS, .SUBCKT, and .INCLUDE but it's easy enough to pipe the awk command to sed and restore the keywords.

Thanks again.
 
  


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] conditional substitution sharky Programming 12 03-28-2010 09:22 AM
conditional operator in c archanac07 Linux - Software 2 02-24-2010 12:58 AM
conditional execution tostay2003 Programming 5 12-28-2007 03:01 PM
c compound conditional steve_96 Programming 9 06-04-2005 07:17 PM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 12:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:24 PM.

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