Linux common commands -- Sed/Grep

Sed replace matched string
sed -i -e 's/original/new/' file
List only file names which match the given pattern
find path -type f -exec grep -l "regex expression" {} \;
sed delete match pattern across multiple lines.

File (alpha.txt)content is as below

            "externalPartyId":[
              {
                "id":[
                  "5036"
                ],
                "externalSystem":[
                  "AOCM"
                ],
                "status":[
                  "Active"
                ],
                "type":[
                  "AOCM Id"
                ]
              }
            ]

Delete content:

,
"type":[
  "AOCM Id"
]

Command:

sed -i -e '/\,$/ {
N
/\"type\"/ {
    N
    /\"Centrelink Card\"/d
  }
}' alpha.txt
find api-tests/test/templates/ -name “*-e” -exec sh -c ‘mv -f 0 0 {0%-e}’ {} \;
Extract substring from line

such as, extract the string followed by internalId. The result should be “659145767487949251

echo "internalPartyId.internalId=659145767487949251;internalPartyId.otherField" | sed 's/.*internalId=\(.*\);internalPartyId.*/\1/g'

output the matched party

grep -o "traceId=abc_.*abc" targetFile

example:

2018-07-25 19:50:29.062[ERROR][service=appName,authType=,appId=,ver=,userId=1234567,traceId=CSP_PTY_MESSAGE_146436488,severity=Low,event=,sourceSystem=] - ab.c.g.ExceptionLoggerServiceImpl - Can’t process further.

Extract value of userId part

grep -o "userId=.*,traceId" --color sourcefile.txt | sed 's/userId=\(.*\),traceId$/\1/g'

Extract value matching the given regular expression

grep -E -o "[0-9]{1,}" sourceFile.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值