awk to lookup stored variable in file and print matching line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to lookup stored variable in file and print matching line
# 1  
Old 01-16-2017
awk to lookup stored variable in file and print matching line

The bash bash below extracts the oldest folder from a directory and stores it in filename
That result will match a line in bold in input. In the matching line there is an_xxx digit in italics that
(once the leading zero is removed) will match a line in link. That is the lint to print in output.
There will always be only one exact match. Thank you Smilie.

Code:
# oldest folder used analysis and version log created
dir=/home/cmccabe/Desktop/NGS/test
{
  read -r -d $'\t' time && read -r -d '' filename
} < <(find "$dir" -maxdepth 1 -mindepth 1 -printf '%T+\t%P\0' | sort -z -r )
printf "The oldest folder is $filename, created on $time and analysis done using v1.3 by $USER at $(date "+%D %r")\n" >> /home/cmccabe/Desktop/NGS/test/log

Result of bash:
R_2017_01_13_14_46_04_user_S5-00580-25-Medexome

input
Code:
http://xxx.xx.xxx.xxx/output/Home/Auto_user_S5-00580-25-Medexome_135_080/plugin_out/FileExporter_out.194/R_2017_01_13_14_46_04_user_S5-00580-25-Medexome.tar.bz2
http://xxx.xx.xxx.xxx/output/Home/Auto_user_S5-00580-24-Medexome_136_078/plugin_out/FileExporter_out.191/R_2017_01_13_12_11_56_user_S5-00580-24-Medexome.tar.bz2

link
Code:
http://xxx.xx.xxx.xxx/output/report/latex/80.pdf
http://xxx.xx.xxx.xxx/output/report/latex/78.pdf

awk attempt with explanation
Code:
awk '{match(VAL=substr($0,RSTART,RLENGTH);match($0,/R*_[0-9]+\//);VAL1=substr($0,RSTART,RLENGTH);gsub(/.*_0|.*_|\/);print' $filename < inputlink > output

explanation
Code:
R_2017_01_13_14_46_04_user_S5-00580-25-Medexome extracted from $filename and matched to line 1 in input (section in bold)

that line has _080 in it (in italics)

the 80 (leading zero always removed), matches line1 in link so that is output

desired output this line matches the result from the bash so it is printed
Code:
http://xxx.xx.xxx.xxx/output/report/latex/80.pdf


Last edited by cmccabe; 01-16-2017 at 07:35 PM.. Reason: added bold, italics and desired output
# 2  
Old 01-16-2017
Not clear if you need this to run as a single script or not, but here is 1 solution:

Code:
# oldest folder used analysis and version log created
dir=/home/cmccabe/Desktop/NGS/test

find "$dir" -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0' | sort -rz |
while read -r -d $'\t' time && read -r -d '' filename
do
    printf "The oldest folder is $filename, created on $time and analysis done using v1.3 by $USER at $(date "+%D %r")\n"
    awk -v FL="$filename" '
         FNR == 1 {filenum++}
         filenum==1 && index($0, FL) { 
              match($0, "_0*([0-9]+)/")
              FNUM=substr($0,RSTART+1,RLENGTH-2)
              gsub(/^0+/,"", FNUM)
          }
          filenum==2 && $0 ~ FNUM".pdf$"' input link > output
    break
done

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-17-2017
Thank you very much, works perfect Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to print field from lookup file in output

The below awk uses $3 and $4 in search as the min and max, then takes each $2 value in lookup and compares it. If the value in lookupfalls within the range in searchthen it prints the entire line in lookup/ICODE]. What I can't seem to figure out is how to print the matching $5 from search on that... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk - Multi-line data to be stored in variable

Greetings Experts, As part of automating the sql generation, I have the source table name, target table name, join condition stored in a file join_conditions.txt which is a delimited file (I can edit the file if for any reason). The reason I needed to store is I have built SELECT list without... (5 Replies)
Discussion started by: chill3chee
5 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

7. Shell Programming and Scripting

print entire line after if lookup

I hope this is a basic question. I have a file with a bunch of strings in each line (and the string number is variable). What I want to do is a simple if command and then print the entire line. something like awk '{if ($3=="yes") print $1,$2,$3,...$X }' infile > outfile Can someone... (1 Reply)
Discussion started by: dcfargo
1 Replies

8. Shell Programming and Scripting

using perl for matching one file with another file and print into new line

One file is fileA 0.0246*0.0068*0.0013*0.0023*0.0182*0.0028*0.0019*0.4750*0.0028*0.0812*0.0123*0.0018*0.0039*0.0020*0.0028*0.0047*0.0139*0.3330*0.0017*0.0072*0.4789... (4 Replies)
Discussion started by: cdfd123
4 Replies

9. Shell Programming and Scripting

print any required line by its line no using awk and its NR variable

how to print any required line by its line no using awk and its NR variable for eg: ------------ 121343 adfdafd 21213sds dafadfe432 adf.adf%adf --------------- requied o/p if give num=3 it print: 21213sds -------------------------------------- (2 Replies)
Discussion started by: RahulJoshi
2 Replies

10. Shell Programming and Scripting

how to get the string stored in a variable in a line???

Hi all, I want to search for a data type in a line.For this in a loop i am checking for $DATA_TYPE in a line using grep.But grep is not able to find when i give this. Can any one tell me how to check string in $DATA_TYPE variable in line usign grep (or) any other way to do the above task. ... (4 Replies)
Discussion started by: jisha
4 Replies
Login or Register to Ask a Question