LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script to remove the text between all curly brackets from text file (https://www.linuxquestions.org/questions/programming-9/shell-script-to-remove-the-text-between-all-curly-brackets-from-text-file-4175633578/)

nirvaanr 07-08-2018 03:32 PM

shell script to remove the text between all curly brackets from text file
 
Hello experts,

I have a text file with lot of curly brackets (both opening { & closing } ). I need to delete them alongwith the text between opening & closing brackets' pair.

For ex: Input:-

Code:

59. Rh1 Qe4 {(Qf5-e4 Qd8-g8+ Kg6-f5
Qg8-h7+ Kf5-e5 Qh7-e7+ Ke5-f5 Qe7-d7+ Qe4-e6 Qd7-h7+ Qe6-g6 Qh7-h3+ Kf5-f6
Qh3-c3+ Kf6-f7 Qc3-b3+ Kf7-f6 Qb3-b6+ Kf6-f7 Qb6-b7+ Kf7-f6 Qb7-c6+ Kf6-f7
Qc6-d7+ Kf7-f6 Rh1-h8 Qg6-g7 Qd7-c6+ Kf6-f5 Qc6xc5+ Bf4-e5 Rh8-h7 Qg7-g6
Qc5-c2+ Kf5-f6 Qc2-c6+ Kf6-f5 Qc6-f3+ Be5-f4 Qf3-h3+ g5-g4 Qh3-d3+ Kf5-f6
Qd3-d8+ Kf6-f5 Qd8-d5+ Kf5-f6 Rh7-h8 Bf4-e5 Rh8-f8+ Kf6-e7 Qd5-d8+ Ke7-e6
Rf8-e8+ Qg6xe8 Qd8xe8+ Ke6-f5 Qe8-d7+ Kf5-g5 Qd7-d8+ Kg5-f5 Qd8xa5 Kf5-f4
Qa5-d2+ Kf4-g3 Qd2-e3+ Kg3-g2 Qe3xe5 g4-g3) -132.53/42 7} 60. Qg8+
{(Qd8-g8+ Kg6-f5 Qg8-h7+ Kf5-e5 Qh7-e7+ Ke5-f5 Qe7-d7+ Qe4-e6 Qd7-h7+
Qe6-g6 Qh7-h3+ Kf5-f6 Qh3-c3+ Kf6-f7 Qc3-b3+ Kf7-f6 Qb3-b6+ Kf6-g7 Qb6-b7+
Kg7-f6 Qb7-c6+ Kf6-g7 Qc6-d7+ Kg7-f6 Rh1-h8 Qg6-g7 Qd7-c6+ Kf6-f5 Qc6xc5+
Bf4-e5 Rh8-h7 Qg7-g6 Qc5-f2+ Kf5-g4 Qf2-e2+ Kg4-f5 Qe2-d3+ Kf5-f6 Qd3-f3+
Be5-f4 Qf3-c6+ Kf6-f5 Qc6-c8+ Kf5-e5 Rh7-e7+ Ke5-d5 Qc8-d7+ Bf4-d6 Qd7-b5+
Kd5-d4 Qb5-c6 Bd6xe7 Qc6xg6 Kd4-e5 Qg6-e8 Ke5-d6 Qe8-b8+ Kd6-c6 Qb8-c8+
Kc6-d5 Qc8-f5+ Kd5-d4 Qf5xa5 Kd4-c4) +132.53/37 4} Kf5



Then the expected output:-

Code:

59. Rh1 Qe4 60. Qg8+ Kf5
I tried sed and awk but failed to do so as I'm not at all good with them. Please help!

Many Thanks!!

nirvaanr 07-08-2018 04:31 PM

I got a solution from here - https://www.unix.com/shell-programmi...#post303019848

Turbocapitalist 07-10-2018 01:59 AM

The example in the link could use a little work on a whitespace problem in its output.

Can you please elaborate a little on what your solution turned out to be? And how well did it work for your full data set?

danielbmartin 07-10-2018 05:50 AM

The two solutions of Chubler_XL produce results with two lines:
Code:

59. Rh1 Qe4  60. Qg8+
 Kf5

The "expected output" posted by OP was one line:
Code:

59. Rh1 Qe4 60. Qg8+ Kf5
A single-line result may be obtained with this code:
Code:

tr " " "\n" <$InFile  \
|sed "/{/,/}/d"      \
|paste -s -d' ' -    \
>$OutFile

Daniel B. Martin

.


All times are GMT -5. The time now is 07:47 AM.