Sunday, April 28, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1360  / 1 Year ago, sat, march 25, 2023, 6:49:08

I have a file like this.


[Guest]
[AAD_987d7f2f57d2]
[mhope]
[SABatchJobs]
[svc-ata]
[svc-bexec]
[svc-netapp]
[dgalanos]
[roleary]
[smorgan]

How to remove the '[' and ']' from the file so i can get the output as below


Guest
AAD_987d7f2f57d2
mhope
SABatchJobs
svc-ata
svc-bexec
svc-netapp
dgalanos
roleary
smorgan

More From » command-line

 Answers
2

Use sed:


sed 's/[][]//g' file


  • The sed command is s/pattern/replacement/modifiers, meaning substitute pattern with replacement. g modifier means globally, otherwise only the first match will be replaced.

  • [...] is a character class, so any character inside the brackets match.

  • ] closes the character class unless its at first place inside. As we want to include that in our character class, we place it first.

  • Replacement is empty in our case.

  • Use sed -i '...' to edit your file in place.




You can also use grep -P like I already suggested in your other question:


grep -Po '[K[^]]*' file

[#793] Sunday, March 26, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
skaing

Total Points: 462
Total Questions: 124
Total Answers: 113

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
skaing questions
;