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
# 1  
Old 01-29-2016
awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you Smilie.

Text file:
Code:
 LastName,FirstName_123456.txt.hg19_multianno

Desired output:
Code:
$1                               $2           $3
LastName,FirstName     123456     data in files (the 24 columns)

Code:
awk '{f=$1; $1=$2=""; sub("  ", ""); print > f}' file


Last edited by vbe; 01-29-2016 at 12:29 PM.. Reason: missing last code tags
# 2  
Old 01-29-2016
Code:
grep "" *

This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-29-2016
If I only wanted the text up to the first . would:
Code:
 grep -o '[[.digit.]].*$' | grep "" *

produce:

Code:
 $1                   $2           $3
LastName,FirstName     123456     data in files (the 24 columns)

with the input being:
Code:
 LastName,FirstName_123456.txt.hg19_multianno

. Thanks Smilie.
# 4  
Old 01-29-2016
Are you saying that each file contains only 24 columns in one line?
Quote:
LastName,FirstName 123456 data in files (the 24 columns)
Let's take a shot at it.
Code:
perl -07 -ne '@np=$ARGV =~/^([^_]*)_(\d+)\./ and print "@np $_"'

This User Gave Thanks to Aia For This Post:
# 5  
Old 01-29-2016
Each text file contains 24 columns with multiple rows in it. I am trying to print the LastName,FirstName in field 1 and the 123456 in field 2 of row 1. Each new filename has a header in row 1. I will post a sample as soon as I can, I am on my blackberry and can not right now. Thank you Smilie.
# 6  
Old 01-29-2016
If I understand what you're trying to do (and I am not at all sure that I do), the following seems to do what you want:
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
}' OFS='\t' *multianno

which, with your attached sample data (stored in a file named LastName,FirstName_123456.txt.hg19_multianno) produces the following as the 1st five lines of its output:
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
LastName,FirstName	123456	1	43394661	43394661	A	exonic	SLC2A1		nonsynonymous SNV	SLC2A1:NM_006516.2:exon8:c.T1016C:p.I339T	0.0002	.	.	.	.	.	0.0001	0.0002	unknown	.	.
LastName,FirstName	123456	2	166870221	166870221	A	intronic	SCN1A				0.01	0.01	.	0.0028	.	0.01	0.0072	0.002	0.0099	.	Common			
LastName,FirstName	123456	9	135802555	135802555	C	intronic	TSC1				0.01	0.0046	.	0.01	.	0.01	0.0075	0.002	0.01	.	Common	untested	Tuberous_sclerosis_database_(TSC1)	TSC1_00008
LastName,FirstName	123456	22	40745898	40745898	C	exonic	ADSL		synonymous SNV	ADSL:NM_000026.3:exon2:c.C216T:p.I72I,ADSL:NM_001123378:exon2:c.C216T:p.I72I	0.01	0.0027	0.002	0.01	.	0.0006	0.0011	0.0003	.

Is this what you're trying to do?

Note, as always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-29-2016
Assuming the following file names:
Code:
ls *unw*
one_1234.unwanted_part  three_3214.unwanted_part  two_2314.unwanted_part

With the following content header, rows and columns:
Code:
cat *unw*
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
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
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

Do you want an output like this?:

Code:
perl -07 -ne '@np=$ARGV =~/^([^_]*)_(\d+)\./ and print "@np\n$_"' *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

Or do you want this output?:
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

Maybe this output?:

Code:
perl -pe '@a=$ARGV =~/^([^_]*)_(\d+)\./; $_="@a $_"' *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
one 1234 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
one 1234 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
one 1234 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
three 3214 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 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 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
two 2314 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 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 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24


Last edited by Aia; 01-29-2016 at 09:13 PM..
This User Gave Thanks to Aia 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 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