Skip to content
Advertisement

How to replace lines that contains integers by using sed

I have a file that contains two fields. The first field may or may not contain an Integer. If it contains an integer and a dot follows the integer I would like to manipulate part of second field. So As an example:

301.    301+Num+Ord          # Change this one 301+Num+Card
100.    100+Num+Ord          # Change this one 100+Num+Card 
3'üncü  3+Num+Ord            # DONOT CHANGE THIS ONE
48.7    48.7+Num+Real        # DONOT CHANGE THIS ONE EITHER . is not at the end
24      24+Num+Card          # DONOT CHANGE THIS ONE No dot
4.      4+Num+Ord          #  Change this one 4+Num+Card
.        .+Punct              # DONOT CHANGE THIS ONE 

I couldn’t make it since I have very limited knowledge in regex. I would be very appreciate if you can help me. thanks

Advertisement

Answer

Could you please try following awk command and let me know if this helps you. Since you haven’t told us which is the next text which you want on place of 2nd column in case it needs to be changed I am simply taking a test string.

awk '($1 ~ /[0-9]+.$/){sub("Ord","Card",$2)} 1' OFS="t" Input_file

Explanation: awk has by default delimiter as space so $1 represents column 1 here. So I am checking here by doing $1 ~ /[0-9]+./$ here if a 1st column is having digits in it followed by a DOT at last of column(. means I am using escape sequence to remove DOT’s special meaning here), if yes then I am substitute ord with card in 2nd field by using sub(substitute) utility of awk here. Then 1 means I am making condition TRUE here and not mentioning and action here so by default action print will happen.

Advertisement