FILENAME of awk with stdin


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FILENAME of awk with stdin
# 1  
Old 08-22-2016
FILENAME of awk with stdin

Hello: How can I print out the FILENAME with awk when the input is from STDIN?
Code:
   zcat SRR1554449.fq.gz  | awk ' (length($2) >= 300) {print FILENAME}'

this will print out
Code:
-
-
-
....

as when awk reads from the standard input, and FILENAME is set to "-". But I am expecting sth like:
Code:
SRR1554449.fq.gz
SRR1554449.fq.gz
SRR1554449.fq.gz
SRR1554449.fq.gz
......

Is there a possible solution for this problem in awk?
Thanks!

Last edited by yifangt; 08-22-2016 at 01:59 PM..
# 2  
Old 08-22-2016
As
Code:
echo SRR1554449.fq.gz  | awk '{print FILENAME}'
-

does print the "file name", I suspect you want SRR1554449.fq.gz to be printed? Without additional measures: you can't, as it is gone when zcat opens it and produces output to be piped into awk.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-22-2016
Another approach:-
Code:
zcat -v SRR1554449.fq.gz 2> out.txt | awk '
                        NR == 1 {
                                getline F < "out.txt"
                        }
                        ( length($2) >= 300 ) {
                                print substr(F,1,index(F,":")-1)
                        }
        '

This User Gave Thanks to Yoda For This Post:
# 4  
Old 08-22-2016
Perhaps you can pass the filename separatedly? For example
Code:
#!/bin/sh
file=SRR1554449.fq.gz
zcat "$file" | awk -v file="$file" ' (length($2) >= 300) {print file}'

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 08-22-2016
Thanks!
Answer by MadeInGermany is what I want.

Last edited by yifangt; 08-22-2016 at 02:26 PM..
# 6  
Old 08-22-2016
I was about to propose sth. like
Code:
file=SRR1554449.fq.gz
{ echo "$file"; zcat "$file"; } | awk 'NR==1'
SRR1554449.fq.gz

This User Gave Thanks to RudiC For This Post:
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 use variable instead of stdin or file

on linux systems, i can do something like this: $ JIOO=hello.one.two $ $ awk -F"." '{print $2}' <<< "${JIOO}" however, on older systems or other unix systems that dont have the fancy stuff this does not work. i contemplated using "-v var="${JIOO}" but i dont think that works. any... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. UNIX for Dummies Questions & Answers

awk for a string in a filename

Hi there I have the variable command below in a script to list the file name in a directory:FILEN="$(ls <PATH> | awk '{print $1}')". When it is run, it correctly returns the following:. I would like to be able to cut the date/time portion of the filename and store it as a variable for later... (4 Replies)
Discussion started by: jimbojames
4 Replies

3. Shell Programming and Scripting

Print filename with awk

I can got the filename with this script. it's only show "-" in result. cut -d , -f7 CSV_d.* | awk 'OFS=":"{print FILENAME,substr($1,1,8),substr($1,9,2),substr($1,11,2),substr($1,13,2)}' | sort |uniq (2 Replies)
Discussion started by: before4
2 Replies

4. UNIX for Dummies Questions & Answers

AWK & FILENAME

My file looks something like this: infile.seq I need to include the filename in each identifier, without the extension, and number them with consecutive numbers starting with 1. So, the expected outfile should look like this: I have been trying to modify the following code but I... (5 Replies)
Discussion started by: Xterra
5 Replies

5. Shell Programming and Scripting

filename change with awk needed

Hi, i have files which contains list of csv file names say temp.txt contains below like data Dns_bangalore_08172011.093033.1139.csv Dns_bangalore_08172011.093133.1139.csv now i want to insert some string before .csv in the filename say i want to insert string _sim1 beofre .csv... (3 Replies)
Discussion started by: raghavendra.nsn
3 Replies

6. Shell Programming and Scripting

awk based on filename

i need to use awk to replace string in file. if suppose filename is abcd.dat i need to replace in file california with CA else if file name name is cdef.dat i need to replace in file california with CALI i need to use awk(must) any suggestion how can i do that file structure ... (3 Replies)
Discussion started by: er_zeeshan05
3 Replies

7. UNIX for Dummies Questions & Answers

Filename Manipulation in AWK

I have created a nawk program and passing a file as an input parameter. The filename will be /home/dir/ksh/test.out I need to extract the filename alone. Is there anyway to get this ? Input : /home/dir/ksh/test.out Output -1: test.out Output -2 : t Input :... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

8. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

9. Shell Programming and Scripting

Dynamic filename in awk

Hi The following code seems to work, but why am i getting an error message? cscyabl@comet:(develop)> awk 'BEGIN {FS="|"}{print $2 >> $1}' test.sum awk: A print or getline function must have a file name. The input line number is 8. The file is test.sum. The source line number is 1. ... (2 Replies)
Discussion started by: Indalecio
2 Replies

10. UNIX for Dummies Questions & Answers

AWK Filename as variable

Need some help. I need to load data into some Oracle tables and one of the pieces of data that I need to load is the filename. This filename is distinct every single time. Basically the last 6 characters will be different with no pattern. ex. testfile_041504_003567 To load the filename... (4 Replies)
Discussion started by: firkus
4 Replies
Login or Register to Ask a Question