Dimon Buzz
Dimon Buzz

Reputation: 1318

sed replace regular expression match

Portion of my dataset which is pipe delimited csv file:

|B20005G |77|B20005G 077|$2,500 to $4,999|
|B20005G |78|B20005G 078|$5,000 to $7,499|
|B20005G |79|B20005G 079|$7,500 to $9,999|

I match the lines of the third field with this sed expression:

sed -n '/|[[:alnum:]]\{7\} [[:digit:]]\{3\}|/p' 

Now, is there a way to tell sed to delete space in the third field to get this:

|B20005G |77|B20005G077|$2,500 to $4,999|
|B20005G |78|B20005G078|$5,000 to $7,499|
|B20005G |79|B20005G079|$7,500 to $9,999|

Upvotes: 0

Views: 433

Answers (2)

Kalanidhi
Kalanidhi

Reputation: 5092

Try this awk method

awk -F'|' 'BEGIN {OFS="|"} {sub(/ +/,"",$4)}1' FileName

OutPut:

|B20005G |77|B20005G077|$2,500 to $4,999|
|B20005G |78|B20005G078|$5,000 to $7,499|
|B20005G |79|B20005G079|$7,500 to $9,999|

Upvotes: 1

Shawn Mehan
Shawn Mehan

Reputation: 4578

with a regex like this

\([[:alnum:]]{7}\) \([[:digit:]]{3}\)

defines two groups, the ones between \( \), which we can refer to in the substitution via \1, \2, so

sed -n 's/\([[:alnum:]]\{7\}\) \([[:digit:]]\{3\}\)/\1\2/' myfile.txt

which gets rid of the space in between the two groups.

Upvotes: 1

Related Questions