LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk delete from end (https://www.linuxquestions.org/questions/programming-9/awk-delete-from-end-763890/)

AutoC 10-23-2009 01:24 AM

awk delete from end
 
I have characters of the form "aa_b_c_d"

I need to repeatedly remove characters from the end,as in
if(aa_b_c_d) do
else if(aa_b_c) do
else if(aa_b) do
else if(aa) do
etc.

how can i do this in a script using awk..i just need to know how to split it like that..

ghostdog74 10-23-2009 02:05 AM

don't understand what you trying to do. show a real example of an input, and show how you want the output to be.

AutoC 10-23-2009 03:34 AM

My inputs are of the form {cluster of characters}_{cluster of characters}_ etc...

eg. aa_bc_dd_ff etc.

Now, I should be able to repeatedly remove the final cluster of characters.

if i supply aa_bc_dd_ff i should get aa_bc_dd
if i supply aa_bc_dd i should get aa_bc
so on

hope thats clear

ghostdog74 10-23-2009 04:03 AM

Code:

$ a="aa_bc_dd_ff"
$ echo "${a%_*}"
aa_bc_dd
$ a="aa_bc_dd"
$ echo "${a%_*}"
aa_bc


David the H. 10-23-2009 08:09 AM

What your really asking is how have awk print a range of fields; and apparently it's not a particularly easy thing to do. I found this page that discusses it:

http://awk.freeshell.org/RangeOfFields

Unfortunately, it looks like the final link on the page for more suggestions is dead, though.


All times are GMT -5. The time now is 02:57 PM.