Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop, while read, - cut? 2

Status
Not open for further replies.

Autosys

Programmer
Jun 1, 2004
90
GB
Hi there,

I'm trying to find an existing post for my problem but can't find one that is similar enough. I normally try quite hard to find a solution myself but was hoping that you can help me. (This will be quite easy I would have thought for UNIX experts like I assume most of you are:

I have a file called: test and the contents of the file look like this:

/apps/data/test/,/apps/book/test,Product,SQL,ODBC

The comma is my field seperator

I need to create a loop that will output each of the fields after the 2nd comma into a new file called result.

The file result should look like this:

Product
SQL
ODBC

Thanks in advance!

S
 
Something like this ?
awk -F, '{for(i=3;i<=NF;++i)print $i}' /path/to/test > result

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Excellent that's done exactly what I wanted it to do. Can I be as annoying to ask you what the steps do? Thanks so much. Looking it up in Unix in a Nutshell as well.
 
what the steps do?
man awk

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
awk -F, '{for(i=3;i<=NF;++i)print $i}' /path/to/test > result

Code:
-F, - Set field separator to ","

'{for(i=3;i<=NF;++i)print $i}'

for - Start FOR loop
(i=3; - Start at Field 3
i<=NF; - If i is less than or equal to total number of fields
++i) - Increase i by one

print $i - Print out the value of i.

/path/to/test - File to perform awk on

> result - Output results to file named.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top