0

I have a file that contains a single line with usernames followed by their other information.

For instance the file contains:

Clara01{25 characters of info}Betty29{25 characters of info}Edith34{25 characters of info}Raji11{25 characters of info}

All in a single very long line with many usernames followed by 25 characters of their information.

Now, I want to search Betty29 and then delete/remove the substring Betty29{25 characters of info}. That is, how should I delete Betty29 and then next 25 characters. How should I do that in Linux shell scripting?

I have read about sed command but I still could not figure out. I am new to shell scripting so please be kind.

5
  • 2
    sed 's/Betty29.\{25\}//' foo.txt Commented Jul 24, 2023 at 6:30
  • Go online, read about regular expressions and sed. Also look within SO Commented Jul 24, 2023 at 6:31
  • 1
    Does this answer your question? delete lines with sed match a special regex Commented Jul 24, 2023 at 6:31
  • @Xavjer This question isn't about deleting lines so I'm going to guess that no it's not a good choice of a dupe Commented Jul 24, 2023 at 15:11
  • @Shawn it worked and gave the output I wanted. Thanks! Commented Jul 25, 2023 at 7:10

3 Answers 3

1

As @Shawn suggested

sed 's/Betty29.\{25\}//' foo.txt 

is getting the job done.

Thanks all for the help!

Sign up to request clarification or add additional context in comments.

2 Comments

Have you read up enough on (POSIX Basic) regular expressions to see how it works?
@Shawn I did read about it. I have a basic idea...
0
  1. Use readarray -d'}' to access the file as an array.
  2. Search for an element starting with Betty29 and unset that element.
  3. Then printf '%s' the whole "${array[@]}" as an output.
unset_element() {
  local -r prefix="$1"
  local -a array
  local -i idx
  readarray -d'}' array
  unset 'array[-1]'  # empty
  for idx in "${!array[@]}"; do
    [[ "${array[idx]}" = "${prefix}{"* ]] && unset 'array[idx]' || :
  done
  printf '%s' "${array[@]}"
}

Now let’s test it:

input='Clara01{25 characters of info}'
input+='Betty29{25 characters of info}'
input+='Edith34{25 characters of info}'
input+='Raji11{25 characters of info}'

unset_element 'Betty29' <<< "$input"

Output:

Clara01{25 characters of info}Edith34{25 characters of info}Raji11{25 characters of info}

Presumably, this removes all occurrences of Betty29. If you want to remove only the first one and make it “more efficient”, just add a break into the for-loop once the match is found.

Comments

0

I wouldn't create a child process (sed or whatever), if I can do it easily within bash as well.

Say we have

line=abcBetty29abcdefghijklmnopqrstuvwxyz

Then we can do

new_line=${line/Betty29?????????????????????????//}

Since counting the 25 question marks is error-prone, an alternative would be to use a regex:

if [[ $line =~ ^(.*)Betty29.{25}(.*) ]]
then
  new_line=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
else
  echo pattern not found 1>&2
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.