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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - remove begin of line up to the third and including occurence of character
# 1  
Old 03-24-2015
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.
# 2  
Old 03-24-2015
With GNU sed:

Code:
$
$ 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
$
$

# 3  
Old 03-24-2015
If I understand your question correctly:
Code:
cat file | cut -d\$  -f4-

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

1234$this$isatest

# 4  
Old 03-25-2015
You could also try something like:
Code:
#!/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:
Code:
1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7
a X b X c X d X e X f X g
[A][B][C][D][E][F][G]
1/2/3/4/5/6/7

it produces the output:
Code:
Using 'X' as delimiter...
1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7
a  d X e X f X g
[A][B][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][B][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][B][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][B][C][D][E][F][G]
14/5/6/7

# 5  
Old 03-25-2015
Maybe a multi-liner is better readable:
Code:
delim='$'
sed "
s/[$delim]//
s/[^$delim]*[$delim]//
s/[^$delim]*[$delim]//
" file

The following two are identical
Code:
cut -d\$ -f4- file

Code:
sed '
s/[^$]*[$]//
s/[^$]*[$]//
s/[^$]*[$]//
' file

# 6  
Old 03-25-2015
Quote:
Originally Posted by MadeInGermany
Maybe a multi-liner is better readable:
Code:
delim='$'
sed "
s/[$delim]//
s/[^$delim]*[$delim]//
s/[^$delim]*[$delim]//
" file

... ... ...
Easier to read? Maybe. Equivalent? No.
Code:
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:
Code:
14$5$6
4$5$6

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 03-25-2015
I meant: the two following samples are identical with each other.
--
Now I see what you mean.

Last edited by MadeInGermany; 03-26-2015 at 05:38 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove bracket including text inside with sed

Hello, I could not remove brackets with text contents myfile: Please remove the bracket with text I wish to remove: I tried: sed 's/\//' myfile It gives: Please remove the bracket with text A1 I expect: Please remove the bracket with text Many thanks Boris (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Bash - sed - Remove first word from line which can begin eventually with blank

hello. How to remove first word from line. The line may or may not start with blank. NEW_PARAM1=$(magic-command " -t --protocol=TCP -P 12345-u root -h localhost ") NEW_PARAM2=$(magic-command "-t --protocol=TCP -P 12345 -u root -h localhost ") I want NEW_PARAM1 equal to NEW_PARAM2 equal ... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

how to remove all text including 2 certain character in each line!

Hi I have a file which has aroun 200 line and it is like this: GROUP2-WDI">GROUP2-WDI GROUP3-WDI">GROUP3-WDI KL2P0508BC">KL2P0508BC KL2P0508BIT">KL2P0508BIT KL3P0506BC">KL3P0506BC KL3P0506BUS">KL3P0506BUS KLD1F0507DBT">KLD1F0507DBT KLD1F0507DIT">KLD1F0507DIT KLD1F0510DBT">KLD1F0510DBT... (3 Replies)
Discussion started by: digitalmahdi
3 Replies

4. Shell Programming and Scripting

sed: remove characters between and including 2 strings

I have the following line: 4/23/2010 0:00:38.000: Copying $$3MSYDDC02$I would like to use sed (or similiar) to remove everthing between and including $ that appears in the line so it ends up like this. 4/23/2010 0:00:38.000: Copying 3MSYDDC02I have been trying these but i'm really just... (5 Replies)
Discussion started by: jelloir
5 Replies

5. Shell Programming and Scripting

sed: remove first character from particular line

Hello Experts, I have a file "tt.txt" which is like: #a1=a2 b1=b2 #c1=c2 I need to remove the pound (#) sign from a particular line. In this case let us assume it's 3rd line : "#c1=c2" I can do it through: sed "s/#c1=c2/c1=c2/" tt.txtbut it is possible that I may not know the value... (6 Replies)
Discussion started by: hkansal
6 Replies

6. UNIX for Dummies Questions & Answers

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution. (5 Replies)
Discussion started by: sumit207
5 Replies

7. Shell Programming and Scripting

sed - remove spaces before 1rst occurence of string

seems easy but havent found in other posts... i want to delete any spaces if found before first occurence of ${AI_RUN} sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
Discussion started by: danmauer
6 Replies

8. UNIX for Advanced & Expert Users

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution.:) (1 Reply)
Discussion started by: sumit207
1 Replies

9. Shell Programming and Scripting

How to use sed to remove html tags including text between them

How to use sed to remove html tags including text between them? Example: User <b> rolvak </b> is stupid. It does not using <b>OOP</b>! and should output: User is stupid. It does not using ! Thank you.. (2 Replies)
Discussion started by: alphagon
2 Replies

10. UNIX for Dummies Questions & Answers

Counting occurence of a particular character on each line

Hi, I have the following data in a flat file: abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 I need to check the no. of occurence of "|" (pipe) on each line and the output should look like below:... (4 Replies)
Discussion started by: hey_mak
4 Replies
Login or Register to Ask a Question