Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 778507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:46:29+00:00 2026-05-14T19:46:29+00:00

Hey I try to write a littel bash script. This should copy a dir

  • 0

Hey I try to write a littel bash script. This should copy a dir and all files in it. Then it should search each file and dir in this copied dir for a String (e.g @ForTestingOnly) and then this save the line number. Then it should go on and count each { and } as soon as the number is equals it should save againg the line number. => it should delete all the lines between this 2 numbers.
I’m trying to make a script which searchs for all this annotations and then delete the method which is directly after this ano.
Thx for help…

so far I have:

echo "please enter dir"
read dir
newdir="$dir""_final"
cp -r $dir $newdir 
cd $newdir

grep -lr -E '@ForTestingOnly' * | xargs sed -i 's/@ForTestingOnly//g'

now with grep I can search and replace the @ForTestingOnly anot. but I like to delete this and the following method…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:46:29+00:00Added an answer on May 14, 2026 at 7:46 pm

    Give this a try. It’s oblivious to braces in comments and literals, though, as David Gelhar warned. It only finds and deletes the first occurrence of the “@ForTestingOnly” block (under the assumption that there will only be one anyway).

    #!/bin/bash
    find . -maxdepth 1 | while read -r file
    do
        open=0 close=0
        # start=$(sed -n '/@ForTestingOnly/{=;q}' "$file")
        while read -r line
        do
            case $line in
                *{*) (( open++ )) ;;
                *}*) (( close++ ));;
                 '') : ;;    # skip blank lines
                  *) # these lines contain the line number that the sed "=" command printed
                     if (( open == close ))
                     then 
                         break
                     fi
                     ;;
            esac
                 # split braces onto separate lines dropping all other chars
                 # print the line number once per line that contains either { or }
        # done < <(sed -n "$start,$ { /[{}]/ s/\([{}]\)/\1\n/g;ta;b;:a;p;=}" "$file")
        done < <(sed -n "/@ForTestingOnly/,$ { /[{}]/ s/\([{}]\)/\1\n/g;ta;b;:a;p;=}" "$file")
        end=$line
        # sed -i "${start},${end}d" "$file"
        sed -i "/@ForTestingOnly/,${end}d" "$file"
    done
    

    Edit: Removed one call to sed (by commenting out and replacing a few lines).

    Edit 2:

    Here’s a breakdown of the main sed line:

    sed -n "/@ForTestingOnly/,$ { /[{}]/ s/\([{}]\)/\1\n/g;ta;b;:a;p;=}" "$file"
    
    • -n – only print lines when explicitly requested
    • /@ForTestingOnly/,$ – from the line containing “@ForTestingOnly” to the end of the file
    • s/ ... / ... /g perform a global (per-line) substitution
    • \( ... \) – capture
    • [{}] – the characters that appear in the list bewteen the square brackets
    • \1\n – substitute what was captured plus a newline
    • ta – if a substitution was made, branch to label “a”
    • b – branch (no label means “to the end and begin the per-line cycle again for the next line) – this branch functions as an “else” for the ta, I could have used T instead of ta;b;:a, but some versions of sed don’t support T
    • :a – label “a”
    • p – print the line (actually, print the pattern buffer which now consists of possibly multiple lines with a “{” or “}” on each one)
    • = – print the current line number of the input file

    The second sed command simply says to delete the lines starting at the one that has the target string and ending at the line found by the while loop.

    The sed command at the top which I commented out says to find the target string and print the line number it’s on and quit. That line isn’t necessary since the main sed command is taking care of starting in the right place.

    The inner whileloop looks at the output of the main sed command and increments counters for each brace. When the counts match it stops.

    The outer while loop steps through all the files in the current directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Probably there isnt any such method. But yes if you… May 14, 2026 at 11:23 pm
  • Editorial Team
    Editorial Team added an answer Firstly, you should be looking at the paging model (Your… May 14, 2026 at 11:23 pm
  • Editorial Team
    Editorial Team added an answer Use the Logger standard library class: require 'logger' logger =… May 14, 2026 at 11:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.