LinuxQuestions.org
Review your favorite Linux distribution.
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 08-01-2008, 06:17 PM   #1
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
awk question


Hello,

How can I get awk to print all fields except the first one...

For example...

I want this

Code:
/path/to/the/file
To output like this...

Code:
/to/the/file
Ive gotten this far
Code:
awk -F'/'
Sorry but I'm a newbie with awk and sed...I should also mention that I am using Sun Solaris (so I do not have the fancy functionality of gsed and gawk )

Any help would be great...thanks

-C

Last edited by custangro; 08-01-2008 at 06:19 PM.
 
Old 08-01-2008, 06:37 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could either loop through 2 to NF printing out the indexed field, or delete $1.

Here is an example:
Code:
 find Documents/LinuxGazette/ | awk -F/ '{ for ( i=3; i<=NF; i++ ) \
{ printf "%s/", $i } print
}' | head
/Documents/LinuxGazette/
issue61/Documents/LinuxGazette/issue61
issue61/okopnik.html/Documents/LinuxGazette/issue61/okopnik.html
issue61/lg_tips61.html/Documents/LinuxGazette/issue61/lg_tips61.html
issue61/andreiana.html/Documents/LinuxGazette/issue61/andreiana.html
issue61/dellomodarme.html/Documents/LinuxGazette/issue61/dellomodarme.html
issue61/index.html/Documents/LinuxGazette/issue61/index.html
issue61/collinge.html/Documents/LinuxGazette/issue61/collinge.html
issue61/ward.html/Documents/LinuxGazette/issue61/ward.html
issue61/lg_backpage61.html/Documents/LinuxGazette/issue61/lg_backpage61.html
I hope this works with a generic awk.

Here I looped from the second to the last field printing out each field. I needed to add the "/" character because the -F/ option is using it as a field separator. After the loop I use a "print" for the new line character.

p.s. And if I used the "-type f" option to the find command, the input to awk would have matched the expected pattern.

Last edited by jschiwal; 08-01-2008 at 06:40 PM.
 
Old 08-01-2008, 06:56 PM   #3
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979

Original Poster
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by jschiwal View Post
You could either loop through 2 to NF printing out the indexed field, or delete $1.

Here is an example:
Code:
 find Documents/LinuxGazette/ | awk -F/ '{ for ( i=3; i<=NF; i++ ) \
{ printf "%s/", $i } print
}' | head
/Documents/LinuxGazette/
issue61/Documents/LinuxGazette/issue61
issue61/okopnik.html/Documents/LinuxGazette/issue61/okopnik.html
issue61/lg_tips61.html/Documents/LinuxGazette/issue61/lg_tips61.html
issue61/andreiana.html/Documents/LinuxGazette/issue61/andreiana.html
issue61/dellomodarme.html/Documents/LinuxGazette/issue61/dellomodarme.html
issue61/index.html/Documents/LinuxGazette/issue61/index.html
issue61/collinge.html/Documents/LinuxGazette/issue61/collinge.html
issue61/ward.html/Documents/LinuxGazette/issue61/ward.html
issue61/lg_backpage61.html/Documents/LinuxGazette/issue61/lg_backpage61.html
I hope this works with a generic awk.

Here I looped from the second to the last field printing out each field. I needed to add the "/" character because the -F/ option is using it as a field separator. After the loop I use a "print" for the new line character.

p.s. And if I used the "-type f" option to the find command, the input to awk would have matched the expected pattern.
That worked perfectly!

So basically you are looping from printing from field 2 to NF and adding / in the process right?

...I think I get it

-C
 
Old 08-01-2008, 07:06 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Here is the second method. The results are a little different. Each line starts with "/" but maybe that is what you want. One thing that needs to be done here is to restore "/" between fields with "OFS="/". Then printing $0 prints "/" between fields:
Code:
find Documents/LinuxGazette/ -type f | awk -F/ 'BEGIN {OFS="/"} { $1=""; print $0}'
The first record is still printed, it's just empty. So this only works if you want remove only the first record. A preceding / on each line will cause different results. In that case, you may need to use the first method. The first record $1 will be empty when it's read in, and in my first example, I would have needed to start with field 3 instead.

Looking back at my first example, while it works, I realize now that I should have expected each line to end with a "/" but using "print" instead of 'printf "\n"' swallowed up that trailing "/" character. I admit it, I got lucky!
---
To answer your question,
Yes, that's just what the first method does.

Last edited by jschiwal; 08-01-2008 at 07:17 PM.
 
Old 08-01-2008, 07:28 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Here is a sed solution that removes the first part.
Code:
find ./Documents/LinuxGazette/ -type f | sed '/^\//s#^/[^/]*/##;/^\/!/s#^[^/]*/##;/^\.\//s#\.\/[^/]*/##'
Like most sed commands, it's easier to write than understand. Since "/" is normal separator in a sed command, I used "#" instead but was stuck with using "/" for the selection tests. I tested for 3 types of matches:
Documents/LinuxGazette/
/Documents/LinuxGazette/
./Documents/LinuxGazette/

The oneliner contains 3 sed commands, separated by a semi-colon. I don't know if this is a gnu extension or not.
/^\//s#^/[^/]*/##
/^\/!/s#^[^/]*/##
/^\.\//s#\.\/[^/]*/##
 
Old 08-01-2008, 08:39 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
another way
Code:
# echo "/path/to/the/file" | awk '{match($0,"/[a-zA-Z0-9]+/");print substr($0,RLENGTH)}'
/to/the/file
 
Old 08-03-2008, 11:51 PM   #7
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
dear custangro

how about the following?

Code:
 sed s/^[^\/]*// inputfile

Last edited by chakka.lokesh; 08-03-2008 at 11:55 PM.
 
  


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk question cmontr Programming 12 11-19-2007 11:35 AM
an awk question zoshr Programming 5 04-13-2007 05:29 AM
awk question davidkline Linux - General 9 09-29-2006 12:37 PM
awk question denalitastic Linux - Newbie 1 06-07-2005 10:42 AM

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

All times are GMT -5. The time now is 12:36 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