Extract a number after a tab as a single line
1
0
Entering edit mode
12 months ago

Hi there, I would like to know how to extract all the numbers after the ID (KC000001-3), including the number set after a tap using Perl regex.

The additional number (0.50) for the first ID, (0.60) second ID, and (0.70 0.80) third ID is always starting with a space as a new line and ending up with another tap.

\s space || \t tab

Input file.

KC000001\s0.30 0.40
\s0.50\t

KC000002\s0.30 0.40 0.50
\s0.60\t

KC152363\s0.30 0.40 0.50 0.60
\s0.70 0.80\t

I would like to get this output file.

output file

0.30 0.40 **0.50**

0.30 0.40 0.50 **0.60**

0.30 0.40 0.50 0.60 **0.70 0.80**

I have prepared this regex.

if ($linea =~ /^(.*[a-z0-9]\d+\.\d)\s(.*?)$/){

print $line
}

However, it is giving me the following error (it is not printing the number after the tab (0.50 for the first), (0.60 for the second), and (0.70 0.80 for the third))

0.30 0.40

0.30 0.40 0.50

0.30 0.40 0.50 0.60

I would like to know what is wrong with this regex. Is it possible to make it with a regex only?

Thanks for your help!

Perl Regex • 721 views
ADD COMMENT
1
Entering edit mode

I formatted your code and you've removed the formatting - please edit it and add it back again. Without it, your post is not very readable.

ADD REPLY
0
Entering edit mode
12 months ago
Zhitian Wu ▴ 60

In your example, all the "records" are separated by a blank line. For each record, you want to have a tabular format of all the "fields".

I will give you several advice

  • first delete all the unnecessary \t in the end of the line
  • use blank line (\n\n) as record separator, in awk it is RS=""
  • there is a new line (\n) in each record, you need to substitute \n to a space , so that all the "fields" are in one line.

Then you can print each record, no regular expression is needed. If you want to know more about regular expression, I would suggest reading books like "Mastering Regular Expressions" or "Classic shell scripting".

awk -v RS='' -v ORS='\n' '{gsub(/\n/,""); print $0}' < file
ADD COMMENT

Login before adding your answer.

Traffic: 1662 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6