Abhishek Sanghvi
Abhishek Sanghvi

Reputation: 13

Remove trailing dot from number and remove round brackets around numbers using sed

I have the following data

X.|X.X|abcdefesd|(XXXX)|XXXXXXXXX

and I'm using sed to format it as

X|X.X|abcdefesd|XXXX|XXXXXXXXX

X = numbers

I am using the command sed "s/\([0-9]+\)/\1/g" for the 1st column ( The numbers can be from 1 digit to 3 digit here)

and sed 's/[^(]*(\([^)]*\)).*/\1/g' for the 4th column ( the number will always be 4 digits)

I am currently new to unix

Upvotes: 1

Views: 187

Answers (3)

Jotne
Jotne

Reputation: 41460

Why not just remove the parentheses?

awk '{gsub(/[()]/,x)}1' <<< "2.|3.5|abcdefesd|(3455)|2412412414"
2.|3.5|abcdefesd|3455|2412412414

Upvotes: 0

devnull
devnull

Reputation: 123608

This might be simpler using awk. Try:

awk -F'|' -vOFS='|' '{$1=sprintf("%d",$1);gsub("[^0-9]","",$4)}1' inputfile

Example:

$ awk -F'|' -vOFS='|' '{$1=sprintf("%d",$1);gsub("[^0-9]","",$4)}1' <<< "2.|3.5|abcdefesd|(3455)|2412412414"
2|3.5|abcdefesd|3455|2412412414

Upvotes: 1

Hari Menon
Hari Menon

Reputation: 35445

This should work:

sed -e 's/\([0-9]\+\)\.\([^0-9]\)/\1\2/g' -e 's/(\([0-9]\+\))/\1/g'

-e is used to chain multiple sed sommands. The first one captures and sequence of digits followed by a dot and then followed by a non-digit, and replaces it without the dot. The second one just replaces any sequence of digits inside brackets with the same sequence without brackets.

echo "2.|3.5|abcdefesd|(3455)|2412412414" | sed -e 's/\([0-9]\+\)\.\([^0-9]\)/\1\2/g' -e 's/(\([0-9]\+\))/\1/g'
2|3.5|abcdefesd|3455|2412412414

Upvotes: 1

Related Questions