LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 02-15-2006, 09:37 AM   #1
manicman
Member
 
Registered: Oct 2005
Location: England (Southampton)
Distribution: Ubuntu 6.06 , Gentoo
Posts: 62

Rep: Reputation: 15
text manipulation in scripts


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
 
Old 02-15-2006, 09:57 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,140
Blog Entries: 54

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
Take input from infile, add separator to the Internal Field Separator variable, turn read variable into array, print last item from array:
Code:
cat infile | while read i; do IFS=">${IFS}"; i=(${i}); echo "${i[$[${#i[@]}-1]]}"; done
 
Old 02-15-2006, 10:16 AM   #3
manicman
Member
 
Registered: Oct 2005
Location: England (Southampton)
Distribution: Ubuntu 6.06 , Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
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
 
Old 02-15-2006, 10:43 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,140
Blog Entries: 54

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
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
Kinda OK:
Code:
awk 'BEGIN { FS=">"}; { print $NF}' infile
 
Old 02-15-2006, 10:56 AM   #5
manicman
Member
 
Registered: Oct 2005
Location: England (Southampton)
Distribution: Ubuntu 6.06 , Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
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
 
Old 02-15-2006, 11:28 AM   #6
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
this is endless....

Code:
$ rev infile | cut -d'>' -f1 | rev
this one uses the cmd rev to reverse the input, so the undertermined index for the last becomes the first, which is cutted and reversed again.
 
Old 02-15-2006, 02:40 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,140
Blog Entries: 54

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
i did try playing with your first post in a script a little (..) everything i alterd seemed to turn out errors
Code:
cut -d ">" -f 1-3 infile

this one uses the cmd rev
Nice one.

Last edited by unSpawn; 02-15-2006 at 02:41 PM. Reason: Avoid useless use of cat
 
Old 02-15-2006, 04:54 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Something like
Code:
awk -F\> '{ for(i=1;i<NF;i++){printf ">"$i}print""}'
should also work :}


Cheers,
Tink
 
Old 02-17-2006, 05:04 AM   #9
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Quote:
Originally Posted by manicman
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
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
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
VPN: Debian Scripts -> Mandriva 2006 Scripts Undefined Mandriva 0 11-30-2005 12:10 PM
Manipulation of text files in C++ Hady Programming 5 05-31-2005 08:24 AM
More text manipulation ice_hockey Linux - General 2 05-28-2005 01:43 AM
CLI based Text manipulation selfsck Linux - General 1 07-15-2004 07:11 PM
Dealing with text in bash scripts Skute Programming 6 03-16-2004 02:58 AM


All times are GMT -5. The time now is 09:51 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration