0

I have some categories and their values:

Category: Value: 1*4 15.934934 1*5*3 19.281281 1*8*10*2 78.20912 

I would like to replace the categories by numbers that represent the categories.

For example:

1*4 has two numbers then it would be replaced by 2

1*5*3 has three numbers then it would be replaced by 3

1*8*10*2 has four numbers then it would be replaced by 4

I need to do the process for hundreds of files so it is very important to automate the process!

3
  • Does it have to be sed? there's a straightforward way in awk for exampleCommentedJul 3, 2017 at 18:08
  • How would you do in using awk? :)CommentedJul 3, 2017 at 18:46
  • 1
    Do a gsub of sequences of digits in $1 and assign the result (number of successful substitutions) back to $1 i.e. $1 = gsub(/[0-9]+/,"",$1)CommentedJul 3, 2017 at 18:51

2 Answers 2

1

Similar to steeldriver's idea, but counting stars:

awk '{ $1 = 1 + gsub(/\*/, "", $1); print; }' < input > output 

When run on the sample input, it results in:

2 15.934934 3 19.281281 4 78.20912 
    0

    As sed has no concept of numbers, this is a bit laborious to do:

    sed -E 's/^[0-9]+\*/1*/; s/^1\*[0-9]+/2/; s/^2\*[0-9]+/3/; s/^3\*[0-9]+/4/;' input 

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.