Linux common commands — Sed/Grep

  • Post author:
  • Post category:linux


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



版权声明:本文为zhangmagle125原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。