![]() |
|
|
grep unix.com with google
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Our Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
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. example an input record containing "picture_001.psd would be output to a filename called "PSD" and it's value would be "picture_001" |
|
|||
|
This would parse all occurences of namex.psd in all the files in a directory (*) and put it in the file PSD: Code:
grep -o '\b[^[:space:]]\+\.psd' * >PSD However they would all have the extension .psd. To cut that off we could do this: Code:
grep -o '\b[^[:space:]]\+\.psd' * |sed 's/\.psd//' >PSD |
|
|||
|
Its a bit wordy but try this: - Code:
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
|
|
|||
|
If the file names have one single dot: Code:
awk -F"." '{print $1 > $2}' file
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| create diffrent files based on other file and parameters list | raghav525 | Shell Programming and Scripting | 9 | 03-18-2009 03:51 AM |
| create 'day' tables based on timestamp in mysql | hazno | UNIX and Linux Applications | 2 | 03-10-2009 07:49 AM |
| loop through two files based on a variable | nogu0001 | Shell Programming and Scripting | 20 | 02-26-2009 05:06 AM |
| create variable name based on another variable's value | benefactr | Shell Programming and Scripting | 2 | 11-01-2007 12:27 PM |