Putting file name inside file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Putting file name inside file
# 1  
Old 04-11-2014
Putting file name inside file

I have a bunch of files with unique names. Inside each file are either 1 or more than 1 rows. I would like the name of the file to appear inside the file itself, once per row (except for the first row which is a header). For example:

Code:
 ls

Code:
dog.1 cat.1 goat.1 tree.1

Code:
cat dog.1

Code:
"grp","V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13","V14","V15","V16"
1,511,513,144,146,0,1,-1,"t","v","v","  kl","    run","bk",40,2,4
2,529,531,162,164,0,1,-1,"q","q","w","  sqw","    go","yr",53,1,3

the output should be:

Code:
"name" "grp","V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13","V14","V15","V16"
"dog.1",1,511,513,144,146,0,1,-1,"t","v","v","  kl","    run","bk",40,2,4
"dog.1",2,529,531,162,164,0,1,-1,"q","q","w","  sqw","    go","yr",53,1,3


Last edited by verse123; 04-11-2014 at 01:47 AM.. Reason: example mistake
# 2  
Old 04-11-2014
Code:
$ awk '{s = FNR==1 ? "\"""name""\"" : "\""FILENAME"\"" ; print s,$0}' OFS=',' infile >outfile

for multiple try

Code:
$ awk 'FNR==1{if(f)close(f);f="Newfile_"FILENAME}{s = FNR==1 ? "\"""name""\"" : "\""FILENAME"\"" ; print s,$0 >f}' OFS=',' file*

Code:
for file in *file; do
        awk '{s = FNR==1 ? "\"""name""\"" : "\""FILENAME"\"" ; print s,$0}' OFS=',' $file >"New_$file"
done

In expected output first line you mentioned space as separator between first 2 fields "name" "grp" is it typo ?

Last edited by Akshay Hegde; 04-11-2014 at 02:05 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 04-25-2014
could you please explain

Code:
for file in *file; do
        awk '{s = FNR==1 ? "\"""name""\"" : "\""FILENAME"\"" ; print s,$0}' OFS=',' $file >"New_$file"
done

# 4  
Old 04-25-2014
Reformatting the code, adding comments, removing some unneeded quotes, and adding a pair of missing quotes, the script could be rewritten as:
Code:
# Loop through every file in the current directory with a filename ending in
# "file" setting the shell variable file to the name of one of those files.
for file in *file
do
        # Run awk for one file...
        awk '
        {       # Set the awk variable s to "name" (in quotes) if this is the
                # 1st line from this file; otherwise set it to the name of the
                # current input file (in quotes).
                s = FNR==1 ? "\"name\"" : "\""FILENAME"\""
                # Print the awk variable s followed by the output field
                # separator followed by the current input line.
                print s,$0
        # Set the output field separator to a comma, reading input from the
        # file named by the shell variable file and directing the
        # output to a file named by the shell variable file preceeded by
        # the string "New_".
        }' OFS=',' "$file" >"New_$file"
done

Does this explain what you wanted to know?

Last edited by Don Cragun; 04-25-2014 at 04:37 AM.. Reason: Fix typos.
# 5  
Old 04-25-2014
If you wanted to combine all of your input files (with names ending in ".1" as in the 1st message in this thread) into a single output file (keeping the heading only from the 1st file), you could also try something like:
Code:
awk '
BEGIN {	# Set output field separator to <comma>
	OFS = ","
}
FNR == 1 {
	# This is the 1st record in a file
	if(NR == 1)	# Is this the 1st line in the 1st file?
		print "\"name\"", $0	# Print header for 1st file only
	next		# Skip to next input line
}
{	# This is not the 1st line in a file
	# Print the quoted input filename and the input line
	print "\"" FILENAME "\"", $0
}' *.1	# Process all file with names ending with ".1"

which with input files like cat.1:
Code:
"grp","V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13","V14","V15","V16"
1,511,513,144,146,0,1,-1,"t","v","v","  kl","    run","bk",40,2,4
2,529,531,162,164,0,1,-1,"q","q","w","  sqw","cat go","yr",53,1,3

and dog.1:
Code:
"grp","V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13","V14","V15","V16"
1,511,513,144,146,0,1,-1,"t","v","v","  kl","    run","bk",40,2,4
2,529,531,162,164,0,1,-1,"q","q","w","  sqw","dog go","yr",53,1,3

produces the output:
Code:
"name","grp","V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13","V14","V15","V16"
"cat.1",1,511,513,144,146,0,1,-1,"t","v","v","  kl","    run","bk",40,2,4
"cat.1",2,529,531,162,164,0,1,-1,"q","q","w","  sqw","cat go","yr",53,1,3
"dog.1",1,511,513,144,146,0,1,-1,"t","v","v","  kl","    run","bk",40,2,4
"dog.1",2,529,531,162,164,0,1,-1,"q","q","w","  sqw","dog go","yr",53,1,3
"goat.1",1,511,513,144,146,0,1,-1,"t","v","v","  kl","    run","bk",40,2,4
"goat.1",2,529,531,162,164,0,1,-1,"q","q","w","  sqw","goatgo","yr",53,1,3
"tree.1",1,511,513,144,146,0,1,-1,"t","v","v","  kl","    run","bk",40,2,4
"tree.1",2,529,531,162,164,0,1,-1,"q","q","w","  sqw","treego","yr",53,1,3

If you want to try this on a Solaris/SunOS system, change awk in the first line of the script to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun 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

Need help in sorting the file and putting to a different folder

Hi, I am new to unix. Kindly help me on this. My requirement is as below: I have a path temp/input , temp/CL and temp/CM I have files in temp/input as below (dates in YYYYMMDDHHMISS) NMP1515O.CL.20181026111213 NMP1515O.CM.20181025111213 ... (4 Replies)
Discussion started by: Shanmugapriya D
4 Replies

2. Shell Programming and Scripting

Putting a separator in file using awk/bash

I have a file with the following content: a-123-345-232 a-23343-4545-545 a-67676-45454-8787 a-129-8912-9824 b-564-78678-2322 b-5454-76767-8899 b-85554-124-152-29 c-34534-654543-323 (... and so on, actually these are pretty huge records) Now, I want that the file should not be broken in to... (8 Replies)
Discussion started by: askerbis
8 Replies

3. Shell Programming and Scripting

Taking part of one file name and putting it into a another file name

Not sure how to do the following, but any help would be appreciated. Has to be done using C shell (sorry about that). I have about 300 files that I need this done for, but I am only going to give one example. I will just need to know how to execute your solution through some type of loop to... (2 Replies)
Discussion started by: jclanc8
2 Replies

4. Shell Programming and Scripting

Putting together all values from different files in one file

Hi All, This is what I am trying to achieve but to no avail. I have three sets of files which are: 1. One big dictionary file which looks like this: apple orange computer pear country 2. Some thousands of text files which are named as 1.dat, 2.dat, 3.dat etc The text files... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

5. Shell Programming and Scripting

putting color on output file script

do you have any simple script on how to change the color and font of a string in a script example echo "====================================" echo " sample color script" echo "====================================" echo " hello " echo " bye" on hello,... (3 Replies)
Discussion started by: lhareigh890
3 Replies

6. Shell Programming and Scripting

deleting from one file and putting into another file

Hi I have a file that is organized like this. Basically the name is indicated by >name, then on a newline there is a sequence of letters. The letters are ASKL however sometimes there are non ASKL's like U's, G's or O's. Basically what I want to do is cut out the UUUU and record them. So heres... (4 Replies)
Discussion started by: gisele_l
4 Replies

7. UNIX for Dummies Questions & Answers

Putting echoed text into a new file

Hi, I've set up a script so that a user answers questions, and then these answers come back onto the screen accompanied by text that I've echoed. Is there a way of putting this into a new file? Thanks (7 Replies)
Discussion started by: likelylad
7 Replies

8. Shell Programming and Scripting

Read data from one file and putting in new file ??

Hello All, I have a file which contain data something like this: CELL 2 TEST AND DIAGNOSTIC UNIT 2 CELL 2, CDM 1 CBR 1 TRANSMIT PORT (TXPORT) 1 CELL 2, CDM 1 CBR 2 TRANSMIT PORT (TXPORT) 1 CELL 2, CDM 1 CBR 3 TRANSMIT PORT (TXPORT) 1 CELL 2, CDM 1 CBR 1 TRANSMIT PORT... (21 Replies)
Discussion started by: wakhan
21 Replies

9. UNIX for Dummies Questions & Answers

putting a timestamp in a file

I was sure there was a way to put a timestamp ina logfile but I can't seem to figure out how. What I would like to do is after the last messages in the rptmgr.err log is put a timestamp so I know the next time I look whats new. I am using AIX 5.1 any help will great Thanks (2 Replies)
Discussion started by: rocker40
2 Replies

10. UNIX for Dummies Questions & Answers

putting restrictions when copying file

I would like to create a command to copy a file with a restriction- if the file exists at the copy destination, the copy does not occur and message is provided that file already exists. (3 Replies)
Discussion started by: thoffpauir
3 Replies
Login or Register to Ask a Question