Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-19-2012
Registered User
 
Join Date: Feb 2006
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
ɹoʇɐɹǝpoɯ
 
Join Date: Feb 2005
Location: Foxborough, MA
Posts: 7,382
Thanks: 112
Thanked 486 Times in 458 Posts

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

Sponsored Links
    #3  
Old 11-19-2012
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,652
Thanks: 164
Thanked 644 Times in 621 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
Registered User
 
Join Date: Feb 2006
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
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,652
Thanks: 164
Thanked 644 Times in 621 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
Registered User
 
Join Date: Feb 2006
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 02:46 PM.