brenda
brenda

Reputation: 803

How to ignore a repeated charcater in a line from a file using bash?

I have a file with n number of lines which look like this:

a,b,c,,,,d
a,b,,,,c,d
a,,,,b,c,d

what I want to do is to delete the repeated commas from each line if there are repeated commas within that line. Therefore, the lines from my file should look like this:

a,b,c,d
a,b,c,d
a,b,c,d

I was trying to use grep or awk but I think I'm not really understanding those commands. I am new at bash and I'm kinda stuck so I would really appreciate your help!!

Upvotes: 1

Views: 38

Answers (3)

Gem Taylor
Gem Taylor

Reputation: 5613

sed is probably the tool for this. Something like:

sed 's/,,*/,/g'

Which you can use with pipes in many ways.

The g option is global ( not m=multiple ) indicating that the phrase can occur more than once on the line - otherwise only the first on each line is modified.

Soon someone will come along and refer us to a duplicate. That's OK.

Upvotes: 3

karakfa
karakfa

Reputation: 67507

this will be shorter

$ tr -s , <file

a,b,c,d
a,b,c,d
a,b,c,d

Upvotes: 1

Timur Shtatland
Timur Shtatland

Reputation: 12347

Use this Perl one-liner to replace 1 or more occurrence of a comma with exactly 1 occurrence, multiple matches per line:

perl -pe 's/,+/,/g' in_file > out_file

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.

Upvotes: 0

Related Questions