sed - remove begin of line up to the third and including occurence of character

hello.
How to remove all characters in a line from first character ( a $ ) until and including the third occurrence of that character ( $ ).

Any help is welcome.

With GNU sed:

$
$ chr="X"
$ str="one X two X three X four X five X six X over"
$
$ echo $str
one X two X three X four X five X six X over
$ echo $str | sed -r "s/$chr([^$chr]+$chr){2}//"
one  four X five X six X over
$
$ # =============
$
$ chr="$"
$ str="one $ two $ three $ four $ five $ six $ over"
$
$ echo $str
one $ two $ three $ four $ five $ six $ over
$ echo $str | sed -r "s/\\$chr([^\\$chr]+\\$chr){2}//"
one  four $ five $ six $ over
$
$

If I understand your question correctly:

cat file | cut -d\$  -f4- 

for example:

echo '$123$a..c$1234$this$isatest' |  cut -d\$  -f4- 

1234$this$isatest

You could also try something like:

#!/bin/ksh
for delim in 'X' '$' ' ' '[' ']' '/'
do	printf "Using '%s' as delimiter...\n" "$delim"
	sed "s/[$delim][^$delim]*[$delim][^$delim]*[$delim]//" file
done

which should work with any sed and any delimiter character. (However, some versions of sed will fail with this if your delimiter is a multibyte character.)

With the input file file containing:

1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7
a X b X c X d X e X f X g
[A][C][D][E][F][G]
1/2/3/4/5/6/7

it produces the output:

Using 'X' as delimiter...
1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7
a  d X e X f X g
[A][C][D][E][F][G]
1/2/3/4/5/6/7
Using '$' as delimiter...
1  4 $ 5 $ 6 $ 7
a X b X c X d X e X f X g
[A][C][D][E][F][G]
1/2/3/4/5/6/7
Using ' ' as delimiter...
1$ 3 $ 4 $ 5 $ 6 $ 7
aX c X d X e X f X g
[A][C][D][E][F][G]
1/2/3/4/5/6/7
Using '[' as delimiter...
1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7
a X b X c X d X e X f X g
C][D][E][F][G]
1/2/3/4/5/6/7
Using ']' as delimiter...
1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7
a X b X c X d X e X f X g
[A[D][E][F][G]
1/2/3/4/5/6/7
Using '/' as delimiter...
1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7
a X b X c X d X e X f X g
[A][C][D][E][F][G]
14/5/6/7

Maybe a multi-liner is better readable:

delim='$'
sed "
s/[$delim]//
s/[^$delim]*[$delim]//
s/[^$delim]*[$delim]//
" file

The following two are identical

cut -d\$ -f4- file
sed '
s/[^$]*[$]//
s/[^$]*[$]//
s/[^$]*[$]//
' file

Easier to read? Maybe. Equivalent? No.

delim='$'
echo '1$2$3$4$5$6' | sed "s/[$delim][^$delim]*[$delim][^$delim]*[$delim]//"

echo '1$2$3$4$5$6' | sed "
s/[$delim]//
s/[^$delim]*[$delim]//
s/[^$delim]*[$delim]//
"

produces:

14$5$6
4$5$6
1 Like

I meant: the two following samples are identical with each other.
--
Now I see what you mean.

Wouldn't the thread title suggest/imply the first s(ubstitute) command to be anchored with ^ ?

That may well be true. The specification in post #1 and the title don't quite match. If we look at it closely, the description in post #1 can be interpreted several ways. It could also be interpreted to mean that instead of using $ as the delimiter, the first character on each input line is the delimiter for that line (and $ was just an example).

And, no sample input and expected output is provided.

Maybe jcdole will clarify the requirements so we'll all know what is really wanted.

Just another idea, take a char which does not occur in your file (here for example "�" ) and use it as a stop mark.
One advantage is that it can be used with string as delimiter.

$ echo fhdsjk123fjdksfj123gjfdklgjdf123jfdkslgfdsl123 | sed 's/123/�/3;s/.*�//'
jfdkslgfdsl123
2 Likes

This is an interesting idea!
The stop mark can even be a new line

echo fhdsjk123fjdksfj123gjfdklgjdf123jfdkslgfdsl123 | sed 's/123/\
/3;s/.*\n//'

In the original problem the delimiter was a $

sed 's/\$/\
/3;s/.*\n//' file