Greping values from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Greping values from a text file
# 1  
Old 12-12-2012
Greping values from a text file

Hi All,
I have 100's of files in the following format. I need to grep or parse out some values from each of the files

Code:
{

 “tree”: “((A:0.2{0},B:0.09{1}):0.7{2},C:0.5{3}){​4};”,

 “placements”:

 [

  {“p”: [[1, −2578.16, 0.777385, 0.004132, 0.0006], [0, −2580.15, 0.107065, 0.000009, 0.0153]], “n”: [“fragment1”]},

  {“p”: [[3, −2576.46, 1.0, 0.003555, 0.000006]], “n”: [“fragment2”]}
],

 “metadata”:

 {“invocation”:

  “pplacer -c tiny.refpkg frags.fasta”

 },

 “version”: 3,

 “fields”:

 [“edge_num”, “likelihood”, “like_weight_ratio”,

    “distal_length”, “pendant_length”]

}

For example; if you look at the vector after  “placements”:, "1, −2578.16, 0.777385, 0.004132, 0.0006" - the "1" represents B in the  “tree”: line (which is the number inside the curly brackets). I just want to parse out the each of the Placement vectors in a tab delimited format along with the number matching the alphabet in the  “tree”: line. Based on the above tree, here is the output I wanted:
Code:
fragment1 B −2578.16 0.777385 0.004132 0.0006
fragment1 A −2580.15 0.107065 0.000009 0.0153
fragment2 C −2576.46, 1.0, 0.003555, 0.000006

Let me know the best way to parse this using awk or sed
# 2  
Old 12-13-2012
try:
Code:
awk '
/tree/ {FS="[\(\),]"; gsub(" ","");
  for (i=1; i<=NF; i++) {
    if ($i ~ /:.*\{/) {
      n=$i; sub(".*[{]","",n); sub("[}].*","",n);
      v=$i; if ($i ~ /.:/) {sub(":.*","",v); l=v; p=v} else {l=p}
      x[n]=v;
    }
  }
}
/fragment/ {FS="[][]" ; a=$(NF-1); gsub("[ .]","",a);
  for (i=1; i<=NF; i++) if ($i ~ /.*,.*,/) {
     s=$i; sub(" *,.*","", s);
     t=$i; sub("^[^,]*, *","", t);
     print a, x[s], t, $i;}
  }
' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare Values in a Delimited Text file

Hi, How do I compare two columns within a text file If 2nd column values are same then I want to know 3rd column number matches or not Example: Prod Stag1 1234.79 Prod Stag2 1234.79 20 Prod Stag3 1234.79 30 Prod Stag4 1234.79 UAT Stag1 1243.56 UAT Stag2 1243.56 20 UAT ... (3 Replies)
Discussion started by: krux_rap
3 Replies

2. UNIX for Beginners Questions & Answers

Qn on awk greping values

Hello Experts, I was trying to awk out some data out of a text file. Below is a sample file which I have xxx ***Wed Jun 28 18:00:59 CDT 2015 avg-cpu: %user %nice %system %iowait %steal %idle 17.10 0.00 4.56 2.86 0.00 75.48 Device: rrqm/s wrqm/s ... (2 Replies)
Discussion started by: DevAnand
2 Replies

3. Shell Programming and Scripting

Text File with Binary Values processing

Hello all, I have a txt file containing millions of lines. Below is the example: {tx:be} head -50 file.txt Instr1: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Instr1:... (6 Replies)
Discussion started by: Zam_1234
6 Replies

4. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

5. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

6. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

7. Shell Programming and Scripting

Removing zero values from text file

Hi all, I wrote the following code to remove the value which are 0 in the input file (a columns if numbers). awk 'BEGIN { for (i=1; i<=NF; i++) if ($i) printf("%13.6e\n",$i) }' $1 >> $2 The script works if the zeros are written as 0.0000 but not as 0.000000e+00 In... (10 Replies)
Discussion started by: f_o_555
10 Replies

8. Shell Programming and Scripting

Greping array values in Bash like Perl

Hi, Is there an easy way to simulate following Perl code in Bash. if ( grep {$my_value eq $_} @ARGV ){ print "Do Something\n"; } else { die "Invalid value"; } (0 Replies)
Discussion started by: paragkalra
0 Replies

9. Shell Programming and Scripting

Assign Values to variables from a text file

The text file has one single row and looks like this Q1 P1 2006 I have to pick up this values from a shell script into three different variables, say quarter, period and year from the above text file. Some one know's how to do this? I went through 'sed', dint really know how to... (3 Replies)
Discussion started by: sarsani
3 Replies
Login or Register to Ask a Question