Extracting information and creating numeric values using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting information and creating numeric values using awk
# 1  
Old 07-19-2010
Extracting information and creating numeric values using awk

I have some variables containing for example

Code:
m02-npt02-sr40-syn-dv0p01-16x12drw
m02-npt02-sr40-syn-dv0p02-16x12drw
m02-npt02-sr40-dv0p03-syn-16x12drw

I want to extract the dv entry for example

Code:
dv0p01
dv0p02
dv0p03

Then I want to convert to a numeric, the p specifies the decimal point

Code:
0.01
 0.02
0.03

and store the values in some other variables
# 2  
Old 07-19-2010
Code:
$ cat in.txt
m02-npt02-sr40-syn-dv0p01-16x12drw
m02-npt02-sr40-syn-dv0p02-16x12drw
m02-npt02-sr40-dv0p03-syn-16x12drw
$ perl -pe 's/.*-dv(\d+)p(\d+)-.*/$1.$2/' in.txt
0.01
0.02
0.03

# 3  
Old 07-19-2010
Any idea how to do it in awk? I have not learned much perl but I will have a look and see how it works.
# 4  
Old 07-19-2010
Code:
nawk -F- '{for(i=1;i<=NF;i++) if ($i~"^dv") {split($i,a,"dv|p");print a[2]"."a[3]}}' myFile

# 5  
Old 07-19-2010
Code:
gawk '{print gensub(/.*-dv([0-9]+)p([0-9]+)-.*/,"\\1.\\2","g")}' urfile

# 6  
Old 07-19-2010
Code:
#!/bin/bash

while read -r LINE
do
  LINE=${LINE##*dv}
  LINE=${LINE%%-*}
  echo ${LINE/p/.}
done <"file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a numeric values in a certain column

Hi All, I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7 I tried awk '{gsub("8","7",$35)}1' infile > outfile ----> not working sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Shell Programming and Scripting

Extracting information using awk

I want to write a script that extracts a value from a line of text. I know it can be done using awk but I've never used awk before so I don't know how to do it. The text is: Mem: 100M Active, 2150K Cache, 500M Buf, 10G Free I want to extract the free memory value to use as a variable. In... (5 Replies)
Discussion started by: millsy5
5 Replies

3. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

4. UNIX for Dummies Questions & Answers

Only print lines with 3 numeric values

Hey guys & gals, I am hoping for some advice on a sed or awk command that will allow to only print lines from a file that contain 3 numeric values. From previous searches here I saw that ygemici used the sed command to remove lines containing more than 3 numeric values ; however how... (3 Replies)
Discussion started by: TAPE
3 Replies

5. Shell Programming and Scripting

Use AWK to check for numeric values? (BASH script)

Hi, I'm quite new to scripting, but know a few AWK statements. I have the following line in my script: hostname=`echo $file | awk 'BEGIN{FS=OFS="."}{$NF=""; NF--; print}'` I use this in my script to rename files, which are similar to this: name.mvkf.mkvfm.mkfvm.1 To the... (4 Replies)
Discussion started by: TauntaunHerder
4 Replies

6. Shell Programming and Scripting

AWK- extracting values from columns, saving them and gettins statistics

Hello, I am obviously quite new to unix and awk. I need to parse certain columns of a file (delimited by spaces), and somehow save the value of this column somewhere, together with the value of the column just after it (by pairs; so something like ). I'm then supposed to count the times that... (9 Replies)
Discussion started by: acsg
9 Replies

7. Shell Programming and Scripting

How to check if the file contains only numeric values

How to check if the file contains only numeric values. I don't want to read entire file it eats lot of cpu Or any way which consumes less memory n cpu.. Please suggest -S (2 Replies)
Discussion started by: sunilmenhdiratt
2 Replies

8. Shell Programming and Scripting

Extracting numeric from string

I have a file whose contents are: $ cat file1 cfd_V03R37 cfd_V03R38 tried sed 's///g' file1 > file2 $cat file1 0337 0338 Is there any way by which i can work on same file and write o/p to the same file instead of using file2 (3 Replies)
Discussion started by: vjasai
3 Replies

9. Programming

numeric values ending in 'U'

I am getting back on the C++ programming after many years away. I recently received an SDK that has code like this where numeric values end in 'U'. What does this mean? if ((ptr % 16U) == 0U) return buffer; (3 Replies)
Discussion started by: sneakyimp
3 Replies

10. Shell Programming and Scripting

Replace spaces with 0's having numeric values.

What could be the regular expression with gsub function in awk to replace all numerics having spaces before to be replaced with 0s? (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question