mrblah
mrblah

Reputation: 103507

regex to remove prefix and another to uppercase the first letter

I need to do things in 2 steps here:

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

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

I am on a Microsoft 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.

Upvotes: 0

Views: 2113

Answers (6)

FMc
FMc

Reputation: 42411

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.

Upvotes: 4

pixelbeat
pixelbeat

Reputation: 31718

This works using GNU sed 4.2.1

sed 's/\[xx_/[/g; s/\[./\U&/g'

Upvotes: 0

Leonardo Herrera
Leonardo Herrera

Reputation: 8406

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.

Upvotes: 3

Amarghosh
Amarghosh

Reputation: 59451

For the first part you can do:

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

Second part:

sed 's/\w\+/\u\0/' filename

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342363

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"

Upvotes: 0

TGV
TGV

Reputation: 866

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.

Upvotes: 0

Related Questions