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.

3 Answers3

1

As @Shawn suggested

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

is getting the job done.

Thanks all for the help!

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
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.

Andrej Podzimek
  • 2,409
  • 9
  • 12
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
user1934428
  • 19,864
  • 7
  • 42
  • 87