unix-text-processing

remove enclosing brackets from a file


How can I efficiently remove enclosing brackets from a file with bash scripting (first occurrence of [ and last occurrence of ] in file)? All brackets that are nested within the outer brackets and may extend over several lines should be retained. Leading or trailing whitespaces may be present.

content of file1

[
  Lorem ipsum
  [dolor] sit [amet
  conse] sadip elitr
]

cat file1 | magicCommand

desired output

  Lorem ipsum
  [dolor] sit [amet
  conse] sadip elitr

content of file2

  [Lorem ipsum [dolor] sit [amet conse] sadip elitr]

cat file2 | magicCommand

desired output

  Lorem ipsum [dolor] sit [amet conse] sadip elitr

Solution

  • If you want to edit the file to remove the braces, use ed:

    printf '%s\n' '1s/^\([[:space:]]*\)\[/\1/' '$s/\]\([[:space:]]*\)$/\1/' w | ed -s file1
    

    If you want to pass on the modified contents of the file to something else as part of a pipeline, use sed:

    sed -e '1s/^\([[:space:]]*\)\[/\1/' -e '$s/\]\([[:space:]]*\)$/\1/' file1
    

    Both of these will, for the first line of the file, remove a [ at the start of the line (Skipping over any initial whitespace before the opening brace), and for the last line of the file (Which can be the same line as in your second example), remove a ] at the end of the line (Not counting any trailing whitespace after the close bracket). Any leading/trailing whitespace will be preserved in the result; use s/...// instead to remove them too.