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 |
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 |
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 \ Code:
awk 'BEGIN { FS=">"}; { print $NF}' infile |
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
|
this is endless....
Code:
$ rev infile | cut -d'>' -f1 | rev |
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. |
Something like
Code:
awk -F\> '{ for(i=1;i<NF;i++){printf ">"$i}print""}' Cheers, Tink |
Quote:
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 |
All times are GMT -5. The time now is 08:44 AM. |