jpseng
jpseng

Reputation: 2210

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

Upvotes: 1

Views: 410

Answers (3)

Ed Morton
Ed Morton

Reputation: 204164

With GNU sed for -E and -z:

$ sed -Ez 's/\[(.*)]/\1/' file1

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

$ sed -Ez 's/\[(.*)]/\1/' file2
  Lorem ipsum [dolor] sit [amet conse] sadip elitr

The above will read the whole file into memory.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247042

perl -0777 -pe 's/^\s*\[\s*//; s/\s*\]\s*$//' file

That's aggressive about removing all whitespace around the outer brackets, which isn't exactly what you show in your desired output.

Upvotes: 1

Shawn
Shawn

Reputation: 52549

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.

Upvotes: 3

Related Questions