LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2009, 01:24 AM   #1
AutoC
Member
 
Registered: Jan 2008
Posts: 30

Rep: Reputation: 15
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..
 
Old 10-23-2009, 02:05 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
don't understand what you trying to do. show a real example of an input, and show how you want the output to be.
 
Old 10-23-2009, 03:34 AM   #3
AutoC
Member
 
Registered: Jan 2008
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 10-23-2009, 04:03 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ a="aa_bc_dd_ff"
$ echo "${a%_*}"
aa_bc_dd
$ a="aa_bc_dd"
$ echo "${a%_*}"
aa_bc
 
Old 10-23-2009, 08:09 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply


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
AWK: delete last chars freeindy Programming 3 07-25-2009 04:35 AM
Can not capture no at end of file store it and print it using sed and awk amuthiga Programming 10 11-26-2008 07:37 AM
How can I strip white space from the start and end of fields using awk? jonnymorris Programming 7 10-02-2008 10:52 PM
Delete lines using awk kkjegan Programming 13 09-11-2007 07:36 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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