LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2004, 04:37 AM   #1
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Rep: Reputation: 30
awk - simple 'for' loop doing my nut...


I want to create an awk script that, passed a URL, trims off the final section (everything after the last '/'

I've created the following in a file trim.awk:

---------------------------

BEGIN { FS="/"
new_var = "" }

{ for (count_var=1; count_var<NF; count_var++) {
new_var = $new_var$count_var
print count_var " - " $count_var " - " $new_var }
}


---------------------------

I'm calling it from the command line with:

echo "www.helloworld.com/this/is/a/test" | awk -f trim.awk

The results I get are far from what I expected:

1 - www.helloworld.com - www.helloworld.com/this/is/a/test
2 - this - www.helloworld.com/this/is/a/test
3 - is - www.helloworld.com/this/is/a/test
4 - a - www.helloworld.com/this/is/a/test

In other words, my $new_var is returning the whole input string every time, regardless of what it's assigned to...

Any ideas? I presume I'm doing something basic wrong, but I've been looking at this for a whole day...

Dave
 
Old 06-30-2004, 07:17 AM   #2
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
After a flash of inspiration and some more googling, I came up with the following solution. The problem was with using $ with variable names...

---------------------------------

BEGIN { FS="/" }

{ for (count_var=1; count_var<NF; count_var++) {
new_var = new_var$count_var"/"
}
print new_var
}

---------------------------------
 
Old 06-30-2004, 08:54 AM   #3
cck23
Member
 
Registered: Jun 2004
Location: Surrey, UK
Distribution: Mandrake 10 Download
Posts: 73

Rep: Reputation: 16
I know the following command was never designed for this but did you consider using:

dirname http:www.linuxquestions.org/billgates.html

However, if you are doing what you are doing as an excercise in awk then fair enough
 
  


Reply



Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple question about sed or awk setianusa Programming 2 09-16-2005 03:57 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
[c shell] simple for loop saiz66 Programming 1 09-28-2004 07:02 PM
Simple for loop lappen Programming 10 09-26-2004 06:36 AM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration