tags:

views:

263

answers:

6

I need to do things in 2 steps here:

  1. modify all occurences of [xx_someText] with [someText]
  2. modify all occurences of [someText] with [SomeText]

I need this in 2 regex because some table names are not prefixed with xx_

I am on a windows machine.

I have access to unix, BUT I am using the GIT bash utility which seems to have sed and perl installed in case that is easer?

UPDATE

Sample Input:

CREATE TABLE [dbo].[xx_attribute]( [attributeID] [int] NOT NULL, [dateTypeID] [tinyint] NOT NULL,

should output as:

CREATE TABLE [dbo].[Attribute]( [AttributeID] [int] NOT NULL, [DateTypeID] [tinyint] NOT NULL

note: the values [int] and [tinyint] will prolly become [Int] and [Tinyint] which is no biggie.

A: 

For the first part you can do:

sed 's/xx_\(\w\+\)/\1/' filename

Second part:

sed 's/\w\+/\u\0/' filename
Amarghosh
A: 

Awk makes this a one step thingy:

awk '{sub("^xx_", ""); print toupper(substr($0, 1, 1)) substr($0, 2);}'

Put your items on a single line. The sub() takes away the prefix, then the print statement prints the first remaining character in uppercase, the rest as it is.

TGV
A: 

This works using GNU sed 4.2.1

sed 's/\[xx_/[/g; s/\[./\U&/g'
pixelbeat
lol, that converted things to: [ b e a l l s p a c e d o u t ]
mrblah
A: 

using just the shell

#!/bin/bash
declare -a arr
while read -r -a arr
do
    for((i=0;i<=${#arr};i++))                    
    do
        case "${arr[i]}" in
            *"[xx_"* );;&
            *"["*)
                arr[i]=${arr[i]//xx_/}
                arr[i]=${arr[i]^^${arr[i]:1:1}}
        esac
    done
    echo ${arr[@]}
done < "file"
That's specific to Bash 4.
Dennis Williamson
yes, so? if OP has it, then its fine. If not, there are other solutions around.
*Yes, so?* is a little glib, given that (1) you never mention that your answer requires a very specific version of Bash and (2) the vast majority of systems do not yet have that version available.
Telemachus
strange, a lot of us never mention Perl, or Python, sed or whatever software versions either, until later maybe. so what's the real problem here? some of you can be so pedantic. WTH
+3  A: 

In Perl:

#!/usr/bin/perl
while (<>) {
    s/\[(?:xx_)?([^]]+)\]/\[\u$1\]/g;
    print;
}

Of course, you can do it from the command line:

 perl -pe 's/\[(?:xx_)?([^]]+)\]/\[\u$1\]/g'

This was tested with your example.

CREATE TABLE [dbo].[xx_SomeAttribute]( [someAttributeID] [int] NOT NULL, [dateTypeID] [tinyint] NOT NULL
CREATE TABLE [Dbo].[SomeAttribute]( [SomeAttributeID] [Int] NOT NULL, [DateTypeID] [Tinyint] NOT NULL

Please note that all text inside brackets is affected.

Leonardo Herrera
hmm..didn't work, it returned: CREATE TABLE [dbo].[xx_SomeAttribute]( [someAttributeID] [int] NOT NULL, [dateTypeID] [tinyint] NOT NULL
mrblah
I did exactly what you wrote, but did a perl -ne '' < input.sql > output.sql
mrblah
Oh, you should update your question then. You want to include the square brackets?
Leonardo Herrera
not sure I follow, my question has both the input and output text with the square brackets e.g. [SomeText] in #2 thanks!
mrblah
updated with sample input
mrblah
did you try it using the 1 liner? hmm...mine not working using GIT bash in win7.
mrblah
I did: perl -pe 's/\[(?:xx_)?([^]]+)\]/\[\u$1\]/g' < input.sql > output.sql and didn't work? very strange!
mrblah
I used single quotes because my shell doesn't like the '$' sign
Leonardo Herrera
now my file is filled with funny characters like 嵛 嬀]
mrblah
Then don't try to use the command line version -- use a script instead.
Leonardo Herrera
+4  A: 
perl -pe "s/\[(xx_)?(\w+)\]/'[' . ($1 ? $2 : ucfirst $2) . ']'/ge"

Double quotes are used here because my understanding is that the OP is on the Windows command line.

FM
Thank you for remind me about '-p'. I'm not used to one liners.
Leonardo Herrera
Good answer, but the xx_ may not be present so that should be: perl -pe 's/(?:xx_)?(\w+)/ucfirst $1/ge'. Also note the single quotes, as you don't want the shell interpolating the $1.
William Pursell
The square brackets are part of the match also
Leonardo Herrera