Home > front end >  How to cut text by given condition (with special characters) in a script
How to cut text by given condition (with special characters) in a script

Time:08-24

I have a file which looks like this (myfile.txt):

{"total":25,"p":1,"ps":100,"paging":{"pageIndex":1,"pageSize":100,"total":25},"issues":[{Lorem Ipsum is simply dummy text of},
{the printing and typesetting industry. Lorem Ipsum has been the industry's},
{standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a},
{type specimen book.}

I want to cut these text:

{"total":25,"p":1,"ps":100,"paging":{"pageIndex":1,"pageSize":100,"total":25},"issues":[

I may use 2 methods here (any method is possible).

  1. I have to remove the line start with {"total" and end with "issues":[ (( including {"total" and "issues":[" ))
  2. I have to remove the all text before "issues":[ text ((including "issues":[ ))

then output would be

{Lorem Ipsum is simply dummy text of},
{the printing and typesetting industry. Lorem Ipsum has been the industry's},
{standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a}
{type specimen book.}

I tried this

cat myfile.txt | sed 's/{"total"//g' | sed 's/"issues":[//g'

but it occurs errors.

Can someone help me to figure out this?

CodePudding user response:

Using sed

$ sed 's/^{"total[^[]*\[//' input_file
{Lorem Ipsum is simply dummy text of},
{the printing and typesetting industry. Lorem Ipsum has been the industry's},
{standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a},
{type specimen book.}

CodePudding user response:

You can use operator s/// of sed for this:

sed -i 's/{"total.*issues":\[\(.*\)/\1/' t.txt
  • {"total: pattern starts with that
  • .*: anything between the beginning and end of pattern
  • issues":\[: the end of the pattern
  • \(.*\): everything else in the rest of the line
  • \1: replaced by the content between parenthesis
  • Related