user3362729
user3362729

Reputation: 21

Replace all the occurences of a characrer after Nth occurence in every line of a file in UNIX

Supposing I have a document in which all lines are formatted like this :

1;23;3453;12312;32423;qwrewre;werwer
ert1;23;jdkj;seiuryy8743;874w65;hjfd;1234
df;23;3453;12312;sdjh;qwrewre;werwer

How do I use sed to replace the all after 2nd occurrences of semicolon(;) with a pipe(|) so that the document will look like:

1;23;3453|12312|32423|qwrewre|werwer
ert1;23;jdkj|seiuryy8743|874w65|hjfd|1234
df;23;3453|12312|sdjh|qwrewre|werwer

I tried using sed 's/;/|/19g but that replaced all the semicolons with pipe.

Thanks for your help in advance.

Upvotes: 2

Views: 126

Answers (1)

jaypal singh
jaypal singh

Reputation: 77185

By doing this:

$ echo '1;23;3453;12312;32423;qwrewre;werwer' | sed 's/;/|/3g'
1;23;3453|12312|32423|qwrewre|werwer

Upvotes: 6

Related Questions