Visit The New, Modern Unix Linux Community

Using awk to create files based on a variable name

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to create files based on a variable name
# 1  
Using awk to create files based on a variable name

Hey all,
I am parsing a file which have records containing one of a number of files names:
".psd", ".cr2", ".crw" , ".cr", ".xi", ".jpg", ".xif" etc
Somewhere on each line there is a value "Namex.psd" "Namex.crw" etc. The position of this name is highly variable
I need to output all the ".psd" records into a single file called PSD. Each record in the file would be named "Namex".
Ultimately I would have 8 or 9 file names containing 15 thousand records.

The question is how do I output data into records based on the contents of the data?

Your help would be greatly appreciated.

---------- Post updated at 11:30 AM ---------- Previous update was at 11:23 AM ----------

One clarification.
I meant each value in the output file would be Namex.
an input record containing "picture_001.psd would be output to a filename called "PSD" and it's value would be "picture_001"
# 2  
This would parse all occurences of namex.psd in all the files in a directory (*) and put it in the file PSD:
grep -o '\b[^[:space:]]\+\.psd' * >PSD

However they would all have the extension .psd. To cut that off we could do this:
grep -o '\b[^[:space:]]\+\.psd' * |sed 's/\.psd//' >PSD

# 3  
ah ha. so I could use a "for" statement and substitute the file type with $i.
for i in psd cr cr2 crw
# 4  
Its a bit wordy but try this: -

nawk '
( match($0, /[ ]+.*(.psd|.cr2|.crw|.cr|.xi|.jpg|.xif) /) ){
        str = substr( $0, RSTART, RLENGTH )
        sub( / $/, "", str)
        sub( /^ /, "", str)
        dot = index(str, ".")
        ext = substr( str, dot + 1, 3)
        tgt = substr( str, 1, dot -1)
        print tgt >> toupper(ext)
' infile

# 5  
If the file names have one single dot:

awk -F"." '{print $1 > $2}' file

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #985
Difficulty: Medium
386BSD and FreeBSD were both derived from BSD releases.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to create subdirectory based on match between two files

In the below awk I am trying to mkdir based of an exact match between file2 line starting with R_2019.... and file1 line starting with R_2019. When a match is found there is a folder located at /home/cmccabe/run with the same name as the match where each $2 in file1 is a new subdirectory in that... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Beginners Questions & Answers

Create file based on data from two other files

I have looked through several threads regarding merging files with awk and attempted using join however have been unsuccessful likely as I do not fully understand awk. What I am attempting is to take a csv file which could be between 1 and 15,000 lines with 5 colums and another csv file that will... (4 Replies)
Discussion started by: cdubu2
4 Replies

3. Shell Programming and Scripting

Create multiple files from single file based on row separator

Hello , Can anyone please help me to solve the below - Input.txt source table abc col1 char col2 number source table bcd col1 date col2 char output should be 2 files based on the row separator "source table" abc.txt col1 char (6 Replies)
Discussion started by: Pratik4891
6 Replies

4. Shell Programming and Scripting

awk Parse And Create Multiple Files Based on Field Value

Hello: I am working parsing a large input file which will be broken down into multiples based on the second field in the file, in this case: STORE. The idea is to create each file with the corresponding store number, for example: Report_$STORENUM_$DATETIMESTAMP , and obtaining the... (7 Replies)
Discussion started by: ec012
7 Replies

5. Shell Programming and Scripting

bash script to create txt files based on timestamp

Hi , please guide me for a bash script that will create a txt files and the name of the txt files will be as of timestamp so that each file name will be different from other and these files will be get created say after every 10 minutes in a folder(/home/p2000/sxs137), please guide me how would... (1 Reply)
Discussion started by: nks342
1 Replies

6. Shell Programming and Scripting

Create files based on second column of a file

Hi All, I have a file which looks like this: 234422 1 .00222 323232 1 3232 32323 1 0.00222 1234 2 1211 2332 2 0.9 233 3 0.883 123 3 45 As you can see, the second column of the file is already sorted which I did using sort command. Now, I want to create files based on the second... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

Awk script to create new file based on previous line

Need help creating a script that does the following: Sort a file Compare the previous line "last field" with current line "last field" If they are the same, print output to a file If they are different print output to a new file The script should keep creating new files if the previous... (5 Replies)
Discussion started by: Muga801
5 Replies

8. UNIX for Advanced & Expert Users

Create a file based on multiple files

Hey everyone. I am trying to figure out a way to create a file that will be renamed based off of one of multiple files. For example, if I have 3 files (cat.ctl, dog.ctl, and bird.ctl) that gets placed on to an ftp site I want to create a single file called,, etc for each... (3 Replies)
Discussion started by: coach5779
3 Replies

9. Shell Programming and Scripting

create diffrent files based on other file and parameters list

I would like ot create shell script/ bash to create diffrent files based on a file and parameters list. Here is the detail example: I have a textfile and four static parameter files (having ?'). mainfile.txt has below records (this count may be more than 50) A200001 A200101 B200001... (9 Replies)
Discussion started by: raghav525
9 Replies

10. Shell Programming and Scripting

create variable name based on another variable's value

Hello, I am needing to create a variable and assign it a value based on the value of a previosly defined variable... I am using KSH.. Example: VAR1=COMPUTER1 I need another variable like ${VAR1}_FLAG="Y", so it would actually be COMPUTER1_FLAG="Y". I will be looping through many values in... (2 Replies)
Discussion started by: benefactr
2 Replies

Featured Tech Videos