Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
hello i am trying to output the last part of a line and im not sure quite how to do this.
for example if i had
Code:
>info1>info2>info3
i would want it to output everything after the last > so it would be info3 there is sometimes more then three > in the line but even then i just want to output the data after the last one can someone help me with this thanks
Thankyou!! that it worked perfectly just one last question how would i alter that so it will do the opposite so instead of outputing everything after the last > it outputed everything before it so it would be
>info1>info2
how would i alter that so it will do the opposite
Why not save the lines to a script, set "set -x", run the script, see what values get assigned where, tinker with it, then post what you have come up with if you can't get it to work.
Anyway. Here's two alternatives, just cuz I feel like posting any:
Baaahhhhd:
Code:
cat infile | while read i; do for n in $(seq 0 "${#i}"); do if [ "$(expr substr \
"${i}" $n 1)" = ">" ]; then v=$n; fi; done; echo ${i:${v}}; done
Thanks for that i went for the awk method (beacuse thats the one i understand the most) i did try playing with your first post in a script a little but to be honist i dont really understand any of it everything i alterd seemed to turn out errors but hopefully with time these things will start to make more sense thanks for all your help
i would want it to output everything after the last > so it would be info3 there is sometimes more then three > in the line but even then i just want to output the data after the last one can someone help me with this thanks
I like sed
The basic structure is something like sed 's///' file
or sed 'search/match/output/' (not literally!)
\(.*\) stands for: match any character
\1 for first match found
\2 second match found
> is the character we are looking for
\(.*\)>\(.*\) is: match1>match2
search/match any character until>anything/output only the first match found/
would be:
Code:
$ sed 's/\(.*\)>\(.*\)/\1/' textfile
>info1>info2
$ sed 's/\(.*\)>\(.*\)/\2/' textfile
info3
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.