The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2009
Registered User
 

Join Date: Nov 2009
Location: San Diego
Posts: 2
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"
  #2 (permalink)  
Old 11-14-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,257
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

  #3 (permalink)  
Old 11-14-2009
Registered User
 

Join Date: Nov 2009
Location: San Diego
Posts: 2
ah ha. so I could use a "for" statement and substitute the file type with $i.
eg:
for i in psd cr cr2 crw
do
  #4 (permalink)  
Old 11-14-2009
Registered User
 

Join Date: Oct 2009
Location: UK
Posts: 224
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

  #5 (permalink)  
Old 11-15-2009
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,667
If the file names have one single dot:


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

Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0