Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > text file modification help

text file modification help
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Nov 12, 2007, 12:12 PM
 
I have a tab-delimited text file that I need to modify. I can use the command line or TextWrangler, whichever would be easier. Each record has three entries separated by tabs and finished with a hard return. I'd like to preface each record with a ">" character and put a hard return after the second field. So it currently looks like:

xxxx xxxx xxxxxxxxxxx

And I would like it to look like:
>xxxx xxxx
xxxxxxxxxxxxx

What would be the easiest way to do this? I know there is some simple command line tool that could handle it.

thanks,
kman
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 12, 2007, 01:07 PM
 
*erased command, didn't work as I originally thought*


where "test" is the name of my file I want to read
(Last edited by besson3c; Nov 12, 2007 at 01:27 PM. )
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 12, 2007, 01:08 PM
 
and if you want to output this to a new file:

*erased command, didn't work as I originally thought* > test2

where test2 is the name of the new file, test is the name of the orig file
(Last edited by besson3c; Nov 12, 2007 at 01:27 PM. )
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 12, 2007, 01:12 PM
 
Ahh... my command is not quite there. Only works with files containing one line.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 12, 2007, 01:18 PM
 
Here you go:

awk {'print ">"$1,$2 "\n" $3'} test


$1 = the first column, $2 = the second column, etc... You may have to change these values if there is stuff before or after the columns you are interested in.
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Nov 12, 2007, 01:18 PM
 
E for effort. Any other unix jockeys out there that can help me out?
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 12, 2007, 01:21 PM
 
My last command works kman, what is the problem with it? WHat output are you getting?
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Nov 12, 2007, 01:43 PM
 
Sorry, I started my post before yours appeared. Thanks.
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Nov 12, 2007, 01:54 PM
 
Hmm...still not working for me. I get the following:

Code:
$ awk {'print ">$1,$2 "\n" $3} tbd0allcopy.tab >
The > symbols continue every time I press return.
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Nov 12, 2007, 01:57 PM
 
When I do a more command on tbd0allcopy.tab I get the following:

225 namehere xxxxxxxxxxxxxxxxxxxxxxxx^M444 name2here cccccccccccccccccccccccccc^M


etc...The fields obviously contain:
225
namehere
xxxxxxxxxxxxxxxxxxxxxxxxx
separated by tabs and a carriage return (^M) at the end of the line.

kman
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 12, 2007, 01:59 PM
 
It looks like the file you are using is using WIndows carriage returns rather than Unix carriage returns, assuming that the ^M is appearing where there should be carriage returns. You'll first need to convert the file from Windows -> Unix CRs...

To do this:

awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt

Lots of other ways to do this though:

How do I convert between Unix and Windows text files? - Knowledge Base
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 12, 2007, 02:00 PM
 
Originally Posted by kman42 View Post
Hmm...still not working for me. I get the following:

Code:
$ awk {'print ">$1,$2 "\n" $3} tbd0allcopy.tab >
The > symbols continue every time I press return.
This is because you are missing the final ' mark after the $3.
     
   
Thread Tools
Forum Links
Forum 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 On
Top
Privacy Policy
All times are GMT -5. The time now is 05:15 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2