0

we are trying to the delete the line that started with "content" word but without success

"content" word actually can start in the begging of file or after spaces or TAB

what is wrong in the sed syntax ?

 sed  "/^[ \t]*"content"\b/Id" file.txt

  "version" : 9,
  "Config" : {
    "cluster_name" : "HDP",
    "stack_id" : "HDP"
  },
  "properties" : {
    "content" : "\n#!/bin/bash\n\n# Set KAFKA specific environment variables here.\n\n# 
  "kafka_user_nproc_limit" : "65536"
.
.

expected output from file.txt

  "version" : 9,
  "Config" : {
    "cluster_name" : "HDP",
    "stack_id" : "HDP"
  },
  "properties" : {
  "kafka_user_nproc_limit" : "65536"
.
.
4

No need for sed if all you're doing is filtering lines based on patterns. That's what grep is for:

grep -iv '^[[:space:]]*"content"' < file.txt

grep -i, as opposed to that I address flag is standard.

Or if that's a proper JSON file, you can use a JSON parsing utility like jq to delete that that key:

jq 'del(.properties.content)' < file.txt
2

I see two issues here:

  1. the double quotes in the pattern won't be matched because you used double quotes to enclose the expression
  2. " is a non-word character, so there will not be a word boundary after it

So

sed  '/^[ \t]*"content"/Id' file.txt

or (if you must use double quotes around the expression)

sed  "/^[ \t]*\"content\"/Id" file.txt
0

we can just do by using below command

sed -e  '/^\s*"content"/d'  file.txt

output

"version" : 9,
  "Config" : {
    "cluster_name" : "HDP",
    "stack_id" : "HDP"
  },
  "properties" : {
  "kafka_user_nproc_limit" : "65536"
.
.
  • Won’t the second expression delete a line that has “content” anywhere in it? That’s not what the requirement is (beginning of line or after spaces or tab). – Jeff Schaller Jun 10 '18 at 23:39
  • @JeffSchaller Command corrected Can you please look into it and let me know for any changes – Praveen Kumar BS Jun 11 '18 at 7:08
  • @PraveenKumarBS sed -e '/^[[:blank:]]*"content"/d' is needed. What you've come up with will remove more than needed lines. firstly the content keyword needs to be enrobed in double quotes which is not the case here. then you also remove lines which have a space followed by the keyword anywhere in the line. – Rakesh Sharma Jun 11 '18 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.