替换字符串并使用Bash将部件移动到结果

时间:2018-05-15 11:11:55

标签: string bash replace

// All methods could be called like this but it will result in a lof of code that is nearly duplicated
//internal void AddOrUpdateClassifications()
//{
//    var result = _IntegrationApiService.GetClassifications();
//
//    //foreach (var value in result)
//    //{
//    //    try
//    //    {
//    //        var current = _DbContext.Classifications.Find(value.ClassificationId);
//    //        if (current == null)
//    //        {
//    //            _DbContext.Classifications.Add(value);
//    //        }
//    //        else
//    //        {
//    //            _DbContext.Entry(current).CurrentValues.SetValues(value);
//    //        }
//    //    }
//    //    catch (Exception e)
//    //    {
//    //        throw;
//    //    }
//    //}
//    //_DbContext.SaveChanges();
//}

internal void AddOrUpdateClassifications()
{
    var result = _IntegrationApiService.GetClassifications();
    GenericAddOrUpdate(result, "ClassificationId");
}

internal void AddOrUpdateFeeCalculations(string businessSystemId, int caseId)
{
    var result = _IntegrationApiService.GetFeeCalculation(businessSystemId, caseId);
    //This does not work
    //GenericAddOrUpdate(result, "BusinessSystemId", "CaseId", "Action", "Cycle", "RateNo");
}

private void GenericAddOrUpdate<T>(ICollection<T> values, params string[] keyValues) where T : class 
{
    foreach (var value in values)
    {
        try
        {
            var keyList = new List<object>();

            foreach (var keyValue in keyValues)
            {
                var propertyInfo = value.GetType().GetProperty(keyValue);
                var propertyValue = propertyInfo.GetValue(value);
                keyList.Add(propertyValue);
            }

            var someDbSet = _DbContext.Set(typeof(T));
            //I need to add all the values from keyList here. I can't add the list directly since I will get the error below. Example with keyList[0] to show working code
            //The specified parameter type 'System.Collections.Generic.List`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, //PublicKeyToken=b77a5c561934e089]]' is not valid. Only scalar types, such as System.Int32, System.Decimal, System.DateTime, and System.Guid, are supported.
            var current = someDbSet.Find(keyList[0]);
            if (current == null)
            {
                someDbSet.Add(value);
            }
            else
            {
                _DbContext.Entry(current).CurrentValues.SetValues(value);
            }
        }
        catch (Exception e)
        {
            throw;
        }
    }
    _DbContext.SaveChanges();
}

需要“{5662}”替换为“[5662](htttps://somelink.com/task/5662)”使用bash

任务编号的计数可能等于或大于4位数

任何想法?

2 个答案:

答案 0 :(得分:2)

使用bash正则表达式和扩展功能

text="TASK {5662}: Task definition"
re='^TASK \{([0-9]{4,})\}: Task definition$'
if [[ $text =~ $re ]]; then
    id=${BASH_REMATCH[1]}
    result=${text/"{$id}"/"[$id](htttps://somelink.com/task/$id)"}
else
    result=
fi

答案 1 :(得分:1)

解决方案1: 关注awk可能对您有所帮助。 (我不是硬编码数字只是通过正则表达式匹配它然后放置新值)

awk --re-interval -v val="[5662](htttps://somelink.com/task/5662)" 'match($0,/[0-9]{4}/){print substr($0,1,RSTART-1) val substr($0,RSTART+RLENGTH+1)}' Input_file

同样--re-interval适用于awk的旧版本,适用于awk的新版本,也可能不需要。{/ p>

解决方案第二: 使用sed硬编码数字:

sed 's#{5662}#[5662](htttps://somelink.com/task/5662)#'  Input_file