naming files in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers naming files in awk
# 1  
Old 09-07-2012
naming files in awk

I have a whole directory and I need each lines of each file to be separated to a new file but I am facing problem naming them Smilie
some of the files even might be empty
the output files should be names original file name + the number of the line or any incremental number

Code:
FILES="data/*"
for X in $FILES
do
	name=$(basename $X) 
	awk '{ print $0 }' $X > “NR-${name}”
done

can you please help me what needs to go instead of NR?
# 2  
Old 09-07-2012
Bug

What about this..Smilie

As per your code - as per your naming conventions
Code:
FILES="data/*"
for X in $FILES
do
    name=$(basename $X) 
    
    awk -v NM="$name"'{ print > NR"-"NM }' $X 
done

Sample

Code:
awk '{ print > "name_"NR }' file


Last edited by pamu; 09-07-2012 at 09:50 AM.. Reason: code added....
# 3  
Old 09-07-2012
Code:
awk '{ print $0 }' $X > “NR-${name}”

"NR" is a awk variable, the bash can't guess it ! If you copy a file with the number of lines you must use wc -l :
Code:
NR=$(wc -l $X)
cp $X $NR-${name}

# 4  
Old 09-07-2012
Delugeag? how would you put that in the line above to do the same thing?
i added but it still does not work and dont know how to work with copy

Code:
FILES="data/*"
for X in $FILES
do
	name=$(basename $X) 
	test=$(wc -l $X)
	awk '{ print $0 }' $X > $test-${name}
done

I need each line of each file to be into new file... is there a chance the method im using is wrong?
# 5  
Old 09-07-2012
Quote:
Originally Posted by A-V
Delugeag? how would you put that in the line above to do the same thing?
i added but it still does not work and dont know how to work with copy
Just see my reply above....Smilie
# 6  
Old 09-07-2012
I saw but it didnt work either Smilie... it only create files equal to the max number of lines in one file for whole directory
oops you changed it one min
in both cases gives a strange error
Code:
awk: data/To-to.txt
awk:           ^ syntax error

# 7  
Old 09-07-2012
Quote:
Originally Posted by A-V
I saw but it didnt work either Smilie... it only create files equal to the max number of lines in one file for whole directory
oops you changed it one min
Just add..

Code:
awk -v NM="$name" '$0 != ""{ print > NR"-"NM }' $X

This User Gave Thanks to pamu 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

Concatenation of files with same naming patterns dynamically

Since my last threads were closed on account of spamming, keeping just this one opened! Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR"... (5 Replies)
Discussion started by: Jesshelle David
5 Replies

2. Shell Programming and Scripting

Help with naming the file

Hi, I have a folder that contains files abc.txt def.txt ....and so on Inside abc.txt, I have @<TRIPOS>MOLECULE 4|Chelerythrine|abcb11_earlyIdentification_Stronginhib_washed_ligprep|sdf|1|dock Inside def.txt, I have @<TRIPOS>MOLECULE... (6 Replies)
Discussion started by: rossi
6 Replies

3. Shell Programming and Scripting

Naming output files based on variable parameters and input filenames

Hello, I have a series of files in sub-directories that I want to loop through, process and name according to the input filename and the various parameters I'm using to process the files. I have a number of each, for example file names like AG005574, AG004788, AG003854 and parameter values like... (2 Replies)
Discussion started by: bdeads
2 Replies

4. UNIX Desktop Questions & Answers

Combining files with specific patterns of naming in a directory

Greetings Unix exports, I am facing some problems in combining files with different name patterns with a directory and I would appreciate if you can help me I have more than 1000 files but they follow a specific pattern of naming. e.g. 64Xtest01.txt They are divided into two sets of test and... (9 Replies)
Discussion started by: A-V
9 Replies

5. Shell Programming and Scripting

Concatenate files to one file with naming convention

Hi , i have below files in wrk folder. file names are 1102090001.CLT 1102090003.CLT 1102100019.CLT 1102100020.CLT the above files are concatenate to one file but that concatenate file name must be same naming convention. (date +%y%m%d)and 0001 count. example : concatenate file... (9 Replies)
Discussion started by: krbala1985
9 Replies

6. Shell Programming and Scripting

Complex overlap and naming of 2 input files - Awk

for every specific $1,$2 check the values ($2,$3) of their E ot I of input1 and overlap with input2. Specify names based on output. ####### if middle value is missing name them "SE" if first value is missing name them "AFE" if last value is missing name them "ALE" if 2 middle values are... (1 Reply)
Discussion started by: ruby_sgp
1 Replies

7. Shell Programming and Scripting

#file naming

hi all, Please advise at what circumstance those file will become -rwxr-xr-x 1 psa psa 1969088 Aug 18 2006 #libaa.sl -rwx------ 1 psa psa 2166784 Jul 25 2006 #libcrypto.sl.0.9.7 -rwx------ 1 psa psa 904040 Jul 25 2006 #libxxx.sl -rwx------ 1 psa ... (2 Replies)
Discussion started by: rauphelhunter
2 Replies

8. UNIX for Dummies Questions & Answers

naming files that csplit creates

Hi, This is my first time on this forum.. I searched the previous answers, but didn't find the answer I was looking for at first glance. csplit works beautifully for me, except for one thing. My file looks like this: ad|name1|asdf...(several pages)..asdf ... ad|name2|asdf...(several... (8 Replies)
Discussion started by: juliette salexa
8 Replies

9. Programming

Naming a socket

Im not very experienced with C so this is probably a basic question. I have a script that opens up 5 sockets, it then runs through a loop and on a given event reconnects to the relevant socket and sends some data. The socket to be reconnected to is kept track of with a 'count' variable. The sockets... (5 Replies)
Discussion started by: geester
5 Replies

10. UNIX for Advanced & Expert Users

Controller Naming

Hello all, How does the Solaris identifies the controller subscript ? ( like c0txdxs0 or c1txdxsx ?? ) I have a unix box ( Ultra 30) running with 2.5.1. When I connected an external hard disk to the on-board scsi port, it got identified as c0t1dxsx... (... (1 Reply)
Discussion started by: shibz
1 Replies
Login or Register to Ask a Question