使用shell变量使用sed查找和替换JSON中的文本

时间:2018-04-16 08:57:57

标签: json bash sed

实际上我的JSON如下所示:

  

“checksum”:“sdkjjfj-shbdjfj23”

我想用另一个值替换校验和值。如上所述,我用过:

  

sed -i's /(“checksum”:“)[^”] *(“)/ \ 1 $ checksumVal \ 2 / g'new.json

new.json更新如下,但我想要该变量的值。

  

“checksum”:“$ checksumVal”,

预期结果:

  

“checksum”:“newval”

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

首先使用一个echo语句并检查$checksumVal是否有某些价值,并尝试下面的内容对我有效。它将如何运作 一旦sed "/checksum找到模式,它将搜索模式"checksum(使用外卡),: "*"将搜索模式: "sdkjjfj-shbdjfj23",此示例为: "$checksumVal",此模式将被替换与 sed "/checksum/s/: ".*"/: \"$checksumVal\"/" new.json

Private Declare PtrSafe Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As LongPtr, ByVal Level As LongPtr, pPrinter As Byte, ByVal Command As LongPtr) As LongPtr

Private Function SetPrinterProperty(ByVal psPrinterName As String, ByVal iPropertyType As LongPtr, ByVal iPropertyValue As LongPtr) As Boolean

'Code adapted from Microsoft KB article Q230743

Dim hPrinter As LongPtr        'handle for the current printer
Dim pd As PRINTER_DEFAULTS
Dim pinfo As PRINTER_INFO_2
Dim dm As DEVMODE
Dim yDevModeData() As Byte         'Byte array to hold contents
                                  'of DEVMODE structure
Dim yPInfoMemory() As Byte        'Byte array to hold contents
                                  'of PRINTER_INFO_2 structure                                      
Dim iBytesNeeded As LongPtr
Dim iRet As LongPtr
Dim iJunk As LongPtr
Dim iCount As LongPtr

'On Error GoTo cleanup

pd.DesiredAccess = PRINTER_ACCESS_USE
'pd.DesiredAccess = PRINTER_ALL_ACCESS
'pd.DesiredAccess = PRINTER_NORMAL_ACCESS

iRet = OpenPrinter(psPrinterName, hPrinter, pd)
If (iRet = 0) Or (hPrinter = 0) Then
   'Can't access current printer. Bail out doing nothing
   Exit Function
End If

'Get the size of the DEVMODE structure to be loaded
iRet = DocumentProperties(0, hPrinter, psPrinterName, 0, 0, 0)    

If (iRet < 0) Then
   'Can't access printer properties.
   GoTo cleanup
End If

'Make sure the byte array is large enough
'Some printer drivers lie about the size of the DEVMODE structure they
'return, so an extra 100 bytes is provided just in case!

'here
ReDim yDevModeData(0 To CLng(iRet) * 24) As Byte

'Load the byte array
iRet = DocumentProperties(0, hPrinter, psPrinterName, CLngPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)

If (iRet < 0) Then
   GoTo cleanup
End If

'Copy the byte array into a structure so it can be manipulated
Call CopyMemory(dm, yDevModeData(0), Len(dm))

If dm.dmFields And iPropertyType = 0 Then
   'Wanted property not available. Bail out.
   GoTo cleanup
End If

'Set the property to the appropriate value
Select Case iPropertyType
Case DM_ORIENTATION
   dm.dmOrientation = CInt(iPropertyValue)
Case DM_PAPERSIZE
   dm.dmPaperSize = CInt(iPropertyValue)
Case DM_PAPERLENGTH
   dm.dmPaperLength = CInt(iPropertyValue)
Case DM_PAPERWIDTH
   dm.dmPaperWidth = CInt(iPropertyValue)
Case DM_DEFAULTSOURCE
   dm.dmDefaultSource = CInt(iPropertyValue)
Case DM_PRINTQUALITY
   dm.dmPrintQuality = CInt(iPropertyValue)
Case DM_COLOR
   dm.dmColor = CInt(iPropertyValue)
Case DM_DUPLEX
   dm.dmDuplex = CInt(iPropertyValue)
End Select

'Load the structure back into the byte array
Call CopyMemory(yDevModeData(0), dm, Len(dm))

'Tell the printer about the new property
iRet = DocumentProperties(0, hPrinter, psPrinterName, yDevModeData(0), yDevModeData(0), DM_IN_BUFFER Or DM_OUT_BUFFER)

If (iRet < 0) Then
   GoTo cleanup
End If

'The code above *ought* to be sufficient to set the property
'correctly. Unfortunately some brands of Postscript printer don't
'seem to respond correctly. The following code is used to make
'sure they also respond correctly.
Call GetPrinter(hPrinter, 2, 0, 0, iBytesNeeded)
If (iBytesNeeded = 0) Then
   'Couldn't access shared printer settings
   GoTo cleanup
End If



'Set byte array large enough for PRINTER_INFO_2 structure
ReDim yPInfoMemory(0 To CLng(iBytesNeeded) + 100) As Byte

'Load the PRINTER_INFO_2 structure into byte array
iRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), iBytesNeeded, iJunk)
If (iRet = 0) Then
   'Couldn't access shared printer settings
   GoTo cleanup
End If

'Copy byte array into the structured type
Call CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))

'Load the DEVMODE structure with byte array containing
'the new property value
pinfo.pDevmode = yDevModeData(0)

'Set security descriptor to null
pinfo.pSecurityDescriptor = 0

'Copy the PRINTER_INFO_2 structure back into byte array
Call CopyMemory(yPInfoMemory(0), pinfo, Len(pinfo))
'Send the new details to the printer

MsgBox hPrinter
MsgBox yPInfoMemory(0)    

iRet = SetPrinter(ByVal hPrinter, 2, yPInfoMemory(0), 0)

'Indicate whether it all worked or not!
SetPrinterProperty = CBool(iRet)

答案 1 :(得分:0)

jq出了什么问题?:

$ cat foo.json # added the required {}
{"checksum": "sdkjjfj-shbdjfj23"}
$ echo $checksum
foo
$ jq '.checksum = "'"$checksum"'"' foo.json
{
  "checksum": "foo"
}