awk to place specific contents filename within text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to place specific contents filename within text file
# 8  
Old 01-30-2016
@Aia the perl below is close except for the 1 in the output goes under column a and the output is tab-deliminated for excel (that was how the original input files was). Thank you Smilie.

Code:
 perl -07 -ne '@np=$ARGV =~/^([^_]*)_(\d+)\./ and print "@np $_"' *unw*
 
one 1234 a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x
         1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
         1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
         1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
three 3214 a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x
            1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
            1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
            1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
two 2314 a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x
         1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
         1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
         1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

---------- Post updated at 07:33 AM ---------- Previous update was at 07:10 AM ----------

@Don Cragun
the awk is all most perfect, except the first two columns field only need to appear in the header row.

Code:
 LastName,FirstName    123456    Chr    Start    End    Ref    Alt    Func.refGene    Gene.refGene    GeneDetail.refGene    ExonicFunc.refGene    AAChange.refGene    PopFreqMax    1000G2012APR_ALL    1000G2012APR_AFR    1000G2012APR_AMR    1000G2012APR_ASN    1000G2012APR_EUR    ESP6500si_ALL    ESP6500si_AA    ESP6500si_EA    CG46    common    clinvar    clinvarsubmit    clinvarreference
                                   1    43394661    43394661    A    exonic    SLC2A1        nonsynonymous SNV    SLC2A1:NM_006516.2:exon8:c.T1016C:p.I339T


Last edited by cmccabe; 01-30-2016 at 09:35 AM.. Reason: added details
# 9  
Old 01-30-2016
Code:
 perl -ne 'BEGIN{$"="\t"}if($.== 1){@np=$ARGV =~/^([^_]*)_(\d+)\./; $sf = length "@np"; print "@np\t$_"}else{ printf "%s\t%s", " "x$sf, $_}; if(eof){$. = 0};' *unw*

Or:
Code:
perl -ne 'BEGIN{$,=$"="\t"}if($fname ne $ARGV){@n=$ARGV =~/^([^_]*)_(\d+)\./; $ind = length "@n"; print @n,$_}else{ print " "x$ind,$_}; $fname=$ARGV' *unw*

Code:
 
one     1234    a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
three   3214    a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
two     2314    a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
                1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Don's awk version can be modified as:
Code:
awk '
FNR == 1 {
	if((n = split(FILENAME, name, "_")) < 2) {
		print "********************************"
		printf("Filename (%s) does not fit expected pattern.\n",
		    FILENAME)
		print "********************************"
		exit 1
	}
	split(name[2], number, ".")
        print name[1], number[1], $0
        gsub(".", " ", name[1])
        gsub(".", " ", number[1])
        next        
}
{	print name[1], number[1], $0
}' OFS='\t' *multianno


Last edited by Aia; 01-30-2016 at 03:59 PM..
# 10  
Old 01-30-2016
Or, without then <space> padding in the 1st two output fields on non-header lines, you could also modify my awk script this way:
Code:
awk '
FNR == 1 {
	if((n = split(FILENAME, name, "_")) < 2) {
		print "********************************"
		printf("Filename (%s) does not fit expected pattern.\n",
		    FILENAME)
		print "********************************"
		exit 1
	}
	split(name[2], number, ".")
	print name[1], number[1], $0
	next
}
{	print "", "", $0
}' OFS='\t' *multianno

# 11  
Old 01-30-2016
Thank you both 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 change specific string to new value if found in text file

I am trying to use awk to change a specific string in a field, if it is found, to another value. In the tab-delimited file the text in bold in $3 contains the string 23, which is always right before a ., if it is present. I am trying to change that string to X, keeping the formatting and the... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

How to merge variable data from another file into specific place?

Hello, I'm trying to create multiple commands using a variable input from another file but am not getting any successful results. Basically, file1.txt contains multiple lines with single words: <file1.txt> yellow blue black white I want to create multiple echo commands with these... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Place the contents of a .CSV file to an array

Hi, I am trying to place the contents of a .CSV file to an array, but not sure how to do that. Here is my .CSV file content: App,SLA,Job name,Avg start time,Avg run time,Frequency,Downstream apps XYZ,,ABC14345,3:00 AM,00.04.00,Daily,STAMP XYZ,9:00,ABC12345,3:15 AM,00.05.00,Daily,STAMP ... (4 Replies)
Discussion started by: ajayakunuri
4 Replies

4. Shell Programming and Scripting

script for inserting line at specific place in file

I use zentyal for my server admin, which is great but zentyal auto-generates config file on boot and hence overwrites any changes made directly to config files. In order to allow multiple user access to a MS ACCESS database, I need to customise the smb.conf file and add the following line to the... (9 Replies)
Discussion started by: barrydocks
9 Replies

5. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

6. Shell Programming and Scripting

Cutting out text from specific portion on filename

Hi, how do I go about cutting out the first numeric characters after the word "access"? access1005101228.merged-00.15.17.86.d8.b8.log.gz (16 Replies)
Discussion started by: GermanJulian
16 Replies

7. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

8. Shell Programming and Scripting

Jump to a specific place in a file?

If I cat a file And want to go to the first instance of a particular value - what command would I use? And then from that point where I jumped to search for another value - but only search from that point forward not before the file? Thanks~ (2 Replies)
Discussion started by: llsmr777
2 Replies

9. Shell Programming and Scripting

Splitting av file in 2 at specific place based on textpattern

I have a file that I want to split in 2 (with Bourne shell sh) preferably. The file consists of groups of lines separated by newline. The file can vary in length, so I need to check number of groups of text. Here's an example ====EXAMPLE START==== #fruit banana #color yellow #surface smooth... (0 Replies)
Discussion started by: borgeh
0 Replies

10. Shell Programming and Scripting

How to adding the filename into file contents

Dear Experts, Please help to teach me how to add the filename into the file content so that i can get the output below:- Actually the file name ***************New output that I want*************** =====2005-11-12===== EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013... (2 Replies)
Discussion started by: missutoomuch
2 Replies
Login or Register to Ask a Question