Using Cut..

Discuss Applications
Post Reply
agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

Using Cut..

Post by agent007 » Mon Mar 08, 2004 3:58 am

hi all,

Can someone pls explain to me cut...I have this huge text file with the foll data:
"acadia.php,,,,,,,Acadia<a><br>"
"afghanistan.php,,,,,,,Afghanistan<a><br>"
"albania.php,,,,,,,Albania<a><br>"
"algeria.php,,,,,,,Algeria<a><br>"
"america.php,,,,,,,America United States<a><br>"
"americansamoa.php,,,,,,,American Samoa<a><br>"
"andorra.php,,,,,,,Andorra<a><br>"
"angola.php,,,,,,,Angola<a><br>"
"anguilla.php,,,,,,,Anguilla<a><br>"
and I need a text file with
Acadia
Afghanistan
Albania
Algeria
America United States
American Samoa
Andorra
Angola
Anguilla
So, what sections need to be cut? The man page doesnt give any examples...


thanks..

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Mar 08, 2004 12:38 pm

If your file is called "myfile.dat":

Code: Select all

$ cut -f8 -d',' myfile.dat | cut -f1 -d'<'
If you want to redirect it to another file just add "> anotherfile.txt" to the end.

agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

Post by agent007 » Tue Mar 09, 2004 12:10 pm

Void,

Thanks! That worked perfectly...Btw, how did you figure that f8 and f1 needed to be cut? Are there 8 fields? How should they be counted?

I cant figure out as to how many fields are there...

thanks..

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Mar 09, 2004 12:29 pm

The number of fields completely depends on what you tell cut to use as the field delimeter. In the first cut command I used "-d ','" which means to split the fields by commas, so the text between the 7th and 8th comma (or in this case everything after the 7th comma since there is no 8th comma) would be the 8th field so I use the "-f 8". What this does is gives you the following stream of data:

Code: Select all

Acadia<a><br>"
Afghanistan<a><br>"
Albania<a><br>"
...
...
So now you have to cut again, this time we want everything before the first "<" symbol. Using "<" as the field delimeter "-d '<'" we would want to pull out the first field "-f 1" resulting in this stream:

Code: Select all

Acadia
Afghanistan
Albania
...
...
It's really quite simple and intuitive once you do it a few times. Then of course you would want to redirect that stream to a file with a redirect symbol ">". Does that clear it up? Maybe the cut man page would be more helpful:

http://voidmain.is-a-geek.net/man/?para ... t&mode=man

If the data stream has fixed columns I would have used the "-c" or "-b" switches instead.

Post Reply