Register
It is currently Wed Oct 22, 2014 4:24 pm

create a file with filename=$5


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Sep 29, 2008 8:38 am   

Joined: Fri Sep 26, 2008 7:58 am
Posts: 10
Hi all. New to this forum so forgive me if I have posted this in the wrong place...if so please feel free to move it or ask for it to be moved.

How would one use awk (or something similar...I just thought awk would be perfect) to create a file with a filename taken from field 5 in the first line?

currently I run this script to extract smaller files from one large text file based on the occurrence of '3' at the end of the line:

Code:
cat $1 |while read line; do
   echo $line |grep $stopword && filecounter=$(( filecounter +1 ))
   echo $line >> table.$filecounter
done


However as you can see this merely creates files named table.1, table.2....table.n.

I would like to get this script to take field 5 from the first line of each new file and use that as the filename. I tried this:

Code:
cat $1 | while read line; do
   echo $line | grep $stopword && filesuffix=$( awk '{print $5}' )
   echo $line >> table$filesuffix
done


However it keeps telling me there is an ambiguous redirect. I would've thought that the "awk" would only come into effect when "grep $stopword" evaluates true and so would therefore only print field 5 from that one line being echoed at that point?

Any suggestions would be most appreciated.


Top
 Profile  
 PostPosted: Wed Oct 01, 2008 8:06 pm   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
I would do it more like...
Code:
< $1 while read line; do
    tablesuffix="$(awk "/$stopword/ { print \\\$5 }")"
    [ -n "$tablesuffix" ] && echo $line > "table_$tablesuffix"
done


Also it's better to stick to one thread for a topic instead of two


Top
 Profile  
 PostPosted: Tue Oct 21, 2008 5:45 am   

Joined: Fri Sep 26, 2008 7:58 am
Posts: 10
No worries sorted it out using awk.

Only took one line.

awk '/stopword$/{close(five".table");five=$5}{print > five".table" } ' $1

But thanks anyway, yours would've worked as well.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP