AWK & FILENAME


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK & FILENAME
# 1  
Old 10-24-2011
AWK & FILENAME

My file looks something like this:

infile.seq
Quote:
>GAGAFFSGSH Freq 20
GACAGTAGCAGTACAGTACAGTAC
>GAGAFFSGjH Freq 10
GACAGTAGCAGTACAGTACAGTAC
>GAGAFFSGSn Freq 5
GACAGTAGCAGTACAGTACAGTAC
>GAGAFFSGSr Freq 2
GACAGTAGCAGTACAGTACAGTAC
>GAGAFFSGSd Freq 1
GACAGTAGCAGTACAGTACAGTAC
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:

Quote:
>infile1 Freq 20
GACAGTAGCAGTACAGTACAGTAC
>infile2 Freq 10
GACAGTAGCAGTACAGTACAGTAC
>infile3 Freq 5
GACAGTAGCAGTACAGTACAGTAC
>infile4 Freq 2
GACAGTAGCAGTACAGTACAGTAC
>infile5 Freq 1
GACAGTAGCAGTACAGTACAGTAC
I have been trying to modify the following code but I cannot get it to do exactly what I want.

Code:
awk '/\>/{F=$2; N=$3; getline}{print ">"FILENAME++" "F" " N"\n" $0}' infile.seq

Any help will be greatly appreciated!
# 2  
Old 10-24-2011
FILENAME is a special variable and already set, incrementing it won't tag a "1" on the end.

How about FILENAME "." N++
# 3  
Old 10-24-2011
Corona

Is there any way I can only include the filename without the extension?
This is the expected outfile:

Quote:
>infile-20 Freq 21
GACAGTAGCAGTACAGTACAGTAC
>infile-10 Freq 11
GACAGTAGCAGTACAGTACAGTAC
>infile-5 Freq 6
GACAGTAGCAGTACAGTACAGTAC
>infile-2 Freq 3
GACAGTAGCAGTACAGTACAGTAC
>infile-1 Freq 2
GACAGTAGCAGTACAGTACAGTAC
Thanks!
# 4  
Old 10-24-2011
Code:
split(FILENAME, A, ".");
print ... > A[1] "-" N++;

---------- Post updated at 10:01 AM ---------- Previous update was at 10:00 AM ----------

That might create FILE- instead of FILE-0... Hmm

Code:
split(FILENAME, A, ".");
print ... > A[1] "-" ++N;

This will create FILE-1, FILE-2, ... instead of FILE-, FILE-1, ...
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-24-2011
Code:
 myprefix=FILENAME; sub ("....$", "", myprefix);

# 6  
Old 10-24-2011
Corona

Got it!

Code:
awk '/\>/{F=$2; N=$3;split(FILENAME, A, "."); getline}{print ">" A[1]"-"N++" "F" " N"\n" $0}' infile.seq

Thanks!

Last edited by Xterra; 10-24-2011 at 01:53 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to find a file based on pattern & return the filename if found?

Hi all, I am a newbie here. I have this requirement to find a file based on a pattern then return the filename if found. I created a script based on online tutorials. Though, I am stuck & really appreciate if anyone can have a quick look & point me to the right direction? #Script starts... (10 Replies)
Discussion started by: buster_t
10 Replies

2. Shell Programming and Scripting

awk Help: quick and easy question may be: How to use &&

Hi Guru's. I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 : awk '{ if ($5>80) && if ($5 != 100) print $0} But getting error: >bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}' syntax error The source line is 1. The error... (6 Replies)
Discussion started by: rveri
6 Replies

3. Shell Programming and Scripting

[Beginner's questions] Filename Validation & Parsing

Hi !! I'm rather new both to the UNIX and scripting worlds, and I'm learning the ropes of scripting. Having said this, please excuse me if you notice certain basic errors. I'm working on a script that implements .jar and .war files for a WAS environment and I need to perform certain... (4 Replies)
Discussion started by: levaldez
4 Replies

4. Shell Programming and Scripting

Korn Shell & Nawk...Filename changes

I have the following piece of code: YESTER=`TZ=aaa24 date +%b"-"%d` filelist2=$(find /export/home/gen/check/logs \( -name \*$YESTER\* ! -name \*ADM\* \) -print | tr '\n' ' ') nawk -F':' ' $2 ~ /Reason/ && $3 !~ /(PASSTHRU|OCAP|FP Power Button|Bootloader Reset)/ { split(FILENAME,... (2 Replies)
Discussion started by: ther2000
2 Replies

5. Shell Programming and Scripting

Filename & Owner

Hi all! I'm trying to write a script that joins the filename and the owner (e.g. .profile_root , home_smith) Is there a easy way to do it. I've been trying with a for and playing with ls and head & tail. I would attach what I've had done but is very ugly:o (1 Reply)
Discussion started by: funyotros
1 Replies

6. UNIX for Dummies Questions & Answers

Adding Date & time stamps to filename

I need to edit the file name with date and time while writing the script. please help. (1 Reply)
Discussion started by: manish.s
1 Replies

7. Shell Programming and Scripting

find and replace in subdirectory (filename & content - both)

Hi, I have to rename all occurance of CUST_MST to RESELLER_MST both in filename and file content under a directory (say D0) which contains multiple (2-3 levels) sub directory. Example: D0 -> D1 -> D2 has a file CUST_MST_TEMP.txt this contains : > cat /D0/D1/D2/CUST_MST_TEMP.txt... (3 Replies)
Discussion started by: sabyasm
3 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

Getting modified time & filename only

Hi, When we use "ls -l" we are getting like below, -rw-r--r-- 1 mdskl mds 4161479 Apr 12 14:57 VTTF2008.20080412145748.cc But i need only modified time and filename only like below, Apr 12 14:57 VTTF3008.20080412145748.cc Thanks-:) Senthil (4 Replies)
Discussion started by: senthil_seera
4 Replies

10. Shell Programming and Scripting

Sort files by Date-timestamps available in filename & pick the sortedfiles one by one

Hi, I am new to Unix shell scripting. Can you please help me with this immediate requirement to code.. The requirement is as given below. In a directory say Y, I have files like this. PP_100000_28062006_122731_746.dat PP_100000_28062006_122731_745.dat PP_100000_28062006_122734_745.dat... (4 Replies)
Discussion started by: Chindhu
4 Replies
Login or Register to Ask a Question