Write 2nd and 3rd fields to a 4th file name? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Write 2nd and 3rd fields to a 4th file name?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-19-2012
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 7 March 2014, 3:13 AM EST
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Write 2nd and 3rd fields to a 4th file name?

I have a flatfile A.txt

Code:
date|products|notes|location
121117|a108|this is a test|florida
121118|b111|just test it|tampa

How do i write an awk to create a file name as location.txt and have products:notes


Code:
awk -F'|' '{ print $2 ":" $3 }' A.txt > $4.txt

I am sure it cannot write to $4.txt; how do you do that?
Thanks.
Sponsored Links
    #2  
Old 11-19-2012
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 2 September 2014, 11:55 PM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts

Code:
awk -F'|' '{ close(o);o=$4 ".txt";print $2,$3 >> o}' OFS=: A.txt

Sponsored Links
    #3  
Old 11-19-2012
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 2 September 2014, 4:09 PM EDT
Location: NM
Posts: 10,188
Thanks: 275
Thanked 785 Times in 734 Posts

Code:
awk -F'|' '{ fname=$4; print $2 ":" $3 > fname; close (fname)}'    A.txt

If you have duplicate $4 field values this will not work.
    #4  
Old 11-19-2012
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 7 March 2014, 3:13 AM EST
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Jim, How do i write output to /some/where/fname.txt?
vgersh99, how do i write it to folder /some/where/o.txt ?
I write to local. If i add path i got syntax erro.
Thanks
Sponsored Links
    #5  
Old 11-19-2012
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 2 September 2014, 4:09 PM EDT
Location: NM
Posts: 10,188
Thanks: 275
Thanked 785 Times in 734 Posts

Code:
# change this:
fname=$4;
#to
fname= "/path/to/some/where" $4;

the space is the concatenation operator in awk.
Sponsored Links
    #6  
Old 11-20-2012
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 7 March 2014, 3:13 AM EST
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract 3rd,4th and 5th element VicNetIT Shell Programming and Scripting 3 06-01-2012 04:40 AM
1st column,2nd column on first line 3rd,4th on second line ect... batcho Shell Programming and Scripting 6 04-06-2012 09:20 AM
search 3 file and write to 4th file (a bit complex) gc_sw Shell Programming and Scripting 7 12-30-2010 03:42 AM
How to extract 3rd line 4th column of a file krishnamurthig Shell Programming and Scripting 4 11-05-2008 04:15 AM
printing 3rd or 4th feild from last in awk. djsal Shell Programming and Scripting 8 05-17-2004 10:46 AM



All times are GMT -4. The time now is 12:37 AM.