Partial content greping into a 3rd file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Partial content greping into a 3rd file
# 1  
Old 02-04-2014
Partial content greping into a 3rd file

Hi,

I do have couple of files in folder. The names of each of the files have a pattern.

Code:
B_A17_A17_1T.txt
B_A17_A17_2T.txt
B_A17_A17_3T.txt
B_A17_A17_7T.txt
.....
.....
B_A17_A17_45T.txt

Each of the above files have the same pattern of data with 4 columns and have an header for the last 3 columns.
Code:
vi B_A17_A17_1T.txt

  A1 A2 A3
1 0 0 0
2 0 1 0.571
3 0.010 6 3.333
4 0 0 0
5 0 0 0
6 0 0 0
7 0.00527 1 1.667
8 0 0 0
9 0 0 0
10 0.0036 1 5.556
11 0 1 1.299
12 0 0 NaN

I also have another single file called file1.txt, that has a single column with 19 rows in it.

Code:
vi file1.txt
A
D
F
G
E
T
G
E
W
T
T
W

I would like to get a third file by greping contents from each of the above files in the following manner.
for example, if we take the file B_A17_A17_1T.txt, I would like to grep part of the file name, A17_1T and print it 12 times as the first column. Then print the content of file1.txt as the second column and then print or grep the third column in the file B_A17_A17_1T.txt. I don't need the headers of the file B_A17_A17_1T.txt.

The sample output file will be a tab delimited file:
Code:
A17_1T A 0
A17_1T D 1
A17_1T F 6
A17_1T G 0
A17_1T E 0
A17_1T T 0
A17_1T G 1
A17_1T E 0
A17_1T W 0
A17_1T T 1
A17_1T T 1
A17_1T W 0

It would be great if I could do this merging and greping of contents for atleast one file and I could repeat the rest. Is awk best for this?
# 2  
Old 02-04-2014
Try (in bash):
Code:
file=B_A17_A17_1T.txt
id=${file#*_};id=${id#*_};id=${id%.*}
paste -d" " <(echo; cat file1.txt) $file | awk 'NR>1{print id, $1, $4}' id=$id

# 3  
Old 02-04-2014
awk -f kan.awk file1.txt B_A17_A17_1T.txt
where kan.awk is:
Code:
function rindex(str,c)
{
  return match(str,"\\" c "[^\\" c "]*$")? RSTART : 0
}

FNR==NR {
  f2[FNR]=$0
  next
}
FNR==1 {f=substr(FILENAME, 1, rindex(FILENAME, "_")-1)}
NF>3{ print f, f2[FNR-1], $3}

# 4  
Old 02-04-2014
Try also:
Code:
awk     'FNR==NR        {T[NR]=$1;next}
         FNR==1         {gsub(/^._[^_]*_/,"",FILENAME); next}
                        {print FILENAME, T[FNR-1], $3}
        ' OFS="\t" file B*

# 5  
Old 02-04-2014
Thanks vgersh 99. It worked fine. but I would like only part of the file name as the first column. A17_1T. At the present, the awk code will give almost all the file name.

RudiC - where do I specify the file names?

Thanks
# 6  
Old 02-04-2014
Another solution using awk:

Code:
awk 'FNR==NR{A[NR]=$0;next}
$1 in A{print substr(FILENAME,3,3) substr(FILENAME,10,3),A[$1],$3}' OFS='\t' file1.txt B_A17_A17_1T.txt

# 7  
Old 02-04-2014
Thanks Chubler_XL. If the file name, B_A17_A17_1T.txt has more character, where do I change the code?

for example instead of B_A17_A17_1T.txt if the file name was Bvtr_A17_A17_1T.txt. Please let me know where to chnage the code. I believe it is in this part of the code
Code:
substr(FILENAME,3,3)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

Replacing partial content of a file.

Hi All, Please help me in the below issue.I had a file called env.prop.In that i need to change service.url.If i select chocie 2. it should come as https://dev2.ecif.info53.com:30102/soap/default 30102 port is not fixed .It varies when the file is updated.So i want to keep orginal port in... (3 Replies)
Discussion started by: bhas85
3 Replies

4. Shell Programming and Scripting

Greping frequency for list of phrases is a separate file

Dear All I have a big set of data which I would like to summerize to have a better sense of it the problem is like this ... I have more than 200 files each related to a person which includes different sentences with specific elements (files in a directory) e.g. mark (one file in the directory)... (9 Replies)
Discussion started by: A-V
9 Replies

5. Shell Programming and Scripting

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 {  “tree”: “((A:0.2{0},B:0.09{1}):0.7{2},C:0.5{3}){​4};”,  “placements”:  , ], “n”: },   {“p”: ], “n”: } ],  “metadata”:  {“invocation”:   “pplacer -c... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

6. Shell Programming and Scripting

Need Help with greping two line from a file Pls help ASAP

Hi all - I''m in a little bit of jam - If you can please help I have a file that has the following content ( please see below) I need to read the file and then get this result in this format ------------- To put out in this format name: sophis Total: 22328 name: tca ... (2 Replies)
Discussion started by: mnassiri
2 Replies

7. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

8. Shell Programming and Scripting

Greping columns data from file.

Hi i am using shell script which perform oracle database query and after that output is redirect to some temporary file. the output of this file looks like SQL*Plus: Release 10.2.0.2.0 - Production on Tue Aug 5 16:08:06 2008 Copyright (c) 1982, 2005, Oracle. All Rights Reserved. ... (6 Replies)
Discussion started by: esungoe
6 Replies

9. Shell Programming and Scripting

problem in greping the string from file using awk

I am using awk command for greping an value from the file the file contains .. file ---------------------------- content----------- -------- String main = "81507066666"; ------------------------------ i am greping the above value using awk command NumberToReplace=`cat "file" | grep... (1 Reply)
Discussion started by: vastare
1 Replies

10. Shell Programming and Scripting

greping date in a file

i have a script that checks inside the file and find the start date and end date with time........... #!/bin/ksh cd /ednadtu3/u01/pipe/logs TZ=`date +%Z`+24 ;b=`date +%Y-%m-%d` echo $b for i in DBMaint.log do echo "Start Time:" >> /ednadtu3/u01/pipe/naveed/Report.txt cat $i | grep... (3 Replies)
Discussion started by: ali560045
3 Replies
Login or Register to Ask a Question