ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 )
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.
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?
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:
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.
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#\.\/[^/]*/##
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.