Bash Script how to use sed to replace text after a number? - bash

{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]}
Im stuck on how I would use sed to remove anything after 66719222 including and then adding ]} at the end. The output should be
This is what I want to delete:
,{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]}
and replace it with ]}
so the output is
{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"}]}
Thanks if anyone could help.

sed 's/,{"Id":66719222.*/]}/' filename
Or if you're willing to sacrifice clarity to make the command one character shorter:
sed 's/,[^}]*66719222.*/]}/' filename
Or more crudely:
sed 's/.\{7\}66719222.*/]}/' filename

Assuming file is your filename.
sed 's/\(.*\),{"Id":66719222.*/\1\]}/' file

If it is in bash, why not just use bash:
str='{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]}'
echo "${x%66719222*}66719222]}"

Related

Use a shell script to replace text with pwd

file.txt
...
<LOCAL_PATH_TO_REPO>/src/java/example.java
...
^A longer file but this pretty much explains what I am trying to do.
script.sh
dir=$(pwd)
# replace <LOCAL_PATH_TO_REPO> with dir
I tried using the sed command but it did not work for some reason. Any ideas on how to do this?
Your error means you have backslashes in the variable text.
The simplest solution is to change the delimiter to the one that does not occur in the variable text.
If there are no commas use a comma:
sed -i "s,LOCAL_PATH_TO_REPO,$PWD," file.yml
The -i flag introduces changes into the input file (works for GNU sed).

Prepend, copy/paste, and append using sed?

I have a file full of IDs which I need to use to build a list of URLs as part of a bash file.
ids.txt is as follows:
s_Foo
p_Bar
s1_Blah
e_Yah
The URLs will always end in a filename that contains the ID, in its own path.
I've looked around for how to prepend and append using sed, but cannot figure out to do the duplicating copy/paste part (\1) using that tool. The ID can be anything, so pattern matching seems hard. Duplication of everything before the line break seems more sensible? I don't know.
How do I create something like this as urls.txt using sed or awk? Is it possible?
https://link.domain.com/list/s_Foo/s_Foo_meta.xml
https://link.domain.com/list/p_Bar/p_Bar_meta.xml
https://link.domain.com/list/s1_Blah/s1_Blah_meta.xml
https://link.domain.com/list/e_Yah/e_Yah_meta.xml
$ sed 's#.*#https://link.domain.com/list/&/&_meta.xml#' ids.txt
https://link.domain.com/list/s_Foo/s_Foo_meta.xml
https://link.domain.com/list/p_Bar/p_Bar_meta.xml
https://link.domain.com/list/s1_Blah/s1_Blah_meta.xml
https://link.domain.com/list/e_Yah/e_Yah_meta.xml
$ awk '{sub(/.*/,"https://link.domain.com/list/&/&_meta.xml")}1' ids.txt
https://link.domain.com/list/s_Foo/s_Foo_meta.xml
https://link.domain.com/list/p_Bar/p_Bar_meta.xml
https://link.domain.com/list/s1_Blah/s1_Blah_meta.xml
https://link.domain.com/list/e_Yah/e_Yah_meta.xml
try gnu sed:
sed -E 's/\S+/https://link.domain.com/list/&/&_meta.xml' ids.txt >urls.txt

Remove leading Zero in CSV file using Unix sed command

My csv file is:
1234,0045,0045,0345,300
1235,0046,0446,2345,301
1236,0047,0447,0350,302
I need the result like this:
1234,45,45,345,300
1235,46,446,2345,301
1236,47,447,350,302
I want to accomplish this task using Unix SED command. Any other alternative method is also appreciated.
Try this :
$ sed 's/^0*\([^,]\)/\1/;s/,0*\([^,]\)/,\1/g' yourfile.csv
1234,45,45,345,300
1235,46,446,2345,301
1236,47,447,350,302

replacing a pattern using sed command

I want to replace a pattern by using sed command. I have a file with below data.
50%%R39%35%R43%-35%R4Z%10%RRN%1110%R0M%-950
Now I need to replace the pattern %RRN%<something>% with %RRN%0%. output should be like this.
50%%R39%35%R43%-35%R4Z%10%RRN%0%R0M%-950
For this I have used below command
sed 's/%RRN%\(.*\)%/%RRN%0%/g'
But I didn't get the correct output. It is coming as below.
50%R39%35%R43%-35%R4Z%10%RRN%0%-950
Please help me on this
Better solution would be use [^%]\+ in place of .*
sed 's/%RRN%\([^%]\+\)%/%RRN%0%/g'
Output:
50%%R39%35%R43%-35%R4Z%10%RRN%0%R0M%-950
If you like to try with awk, you can do:
echo '50%%R39%35%R43%-35%R4Z%10%RRN%1110%R0M%-950' | awk '{sub(/%RRN%[^%]*%/,"%RRN%0%")}1'
50%%R39%35%R43%-35%R4Z%10%RRN%0%R0M%-950
perl -pe '$a="0%";s/(%RRN%)[^%]*%/$1$a/g'

Sed : how to print quotes in the middle of a string and at the end

I am working on Windows, but Unix example will help me.
I am trying to have this type of input
\\server\share
Output
\\server\"share"
I can add quotes at the end if I do it by sed "$s/.""/"
But I don't know how to do it after the last slash.
Help appreciated
\\server\/"share/"
Not sure if that's what you are looking for
This might work for you (GNU sed):
sed -r 's/(.*\\)(.*)/\1"\2"/' file
Which uses greed to locate the last \ and then enclose the remaining string with double-quotes.
Or:
sed -r 's/[^\\]+$/"&"/' file
Which works backwards through the line until it finds a \.

Resources