LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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,499
Blog Entries: 2

Rep: Reputation: 68
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: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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: 38
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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