LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   text manipulation in scripts (https://www.linuxquestions.org/questions/linux-newbie-8/text-manipulation-in-scripts-415705/)

manicman 02-15-2006 09:37 AM

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

unSpawn 02-15-2006 09:57 AM

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

manicman 02-15-2006 10:16 AM

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

unSpawn 02-15-2006 10:43 AM

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

manicman 02-15-2006 10:56 AM

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

marozsas 02-15-2006 11:28 AM

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.

unSpawn 02-15-2006 02:40 PM

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.

Tinkster 02-15-2006 04:54 PM

Something like
Code:

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


Cheers,
Tink

muha 02-17-2006 05:04 AM

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 :D

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



All times are GMT -5. The time now is 08:44 AM.