LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-10-2011, 03:37 PM   #1
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Rep: Reputation: 16
Question Trim operation using awk


I wrote a couple of scripts some time back and remember using following code to trim the extra spaces
Code:
echo "single = word" | awk -F'=' '{gsub(/^[[:blank:]]+/,"",$2) gsub(/[[:blank:]]+$/,"",$2); print $2}'
I think it used to work fine but it's not working now.

Any suggestions, why [[:blank:]] thing is not working but [ \t] is ?

PS: In case it matters, I wrote this when I used to work on Fedora. Have been using Ubuntu for the past one year now
 
Old 10-10-2011, 03:47 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
You probably have mawk (minimal awk) installed on Ubuntu, and gawk on Fedora. mawk doesn't support [[:character class:]].
 
Old 10-10-2011, 08:20 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Definitely the first piece of software to be changed on any Ubuntu installation (IMHO).

May I also add that the following would achieve your solution easier:
Code:
echo "single = word" | awk '{print $NF}'
By default gawk uses whitespace as the delimiter and so would remove all from each item on the line.
 
Old 10-10-2011, 08:36 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
The things you learn (mawk) - shows how little I use Ubuntu ...
 
Old 10-11-2011, 04:35 AM   #5
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
Talking

Quote:
Originally Posted by ntubski View Post
You probably have mawk (minimal awk) installed on Ubuntu, and gawk on Fedora. mawk doesn't support [[:character class:]].
Yups !! mawk was playing naughty
All set now. Thanx

Just thinking loud - Had I not added that 'PS' note just before posting this, we could have gone on a totally different 'Pursuit of AWKwardness'

Last edited by luvshines; 10-11-2011 at 04:38 AM.
 
Old 10-11-2011, 08:08 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Not really ... tends to be a question I ask as soon as I see Ubuntu and awk issues.
 
Old 10-11-2011, 03:17 PM   #7
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
Not really ... tends to be a question I ask as soon as I see Ubuntu and awk issues.
And that is exactly what I was missing from my post when I was posting it. Just before pressing the 'submit' button, it struck that the script were originally written Fedora and I then added the PS note
 
  


Reply



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
numerical operation on selected lines and column using AWK program vjramana Linux - Newbie 3 05-16-2010 11:43 PM
Arithmetic operation with awk fugitive569 Programming 2 12-04-2009 10:10 AM
row operation usin awk RudraB Programming 3 03-02-2008 04:42 AM
How To Trim A File fpfernando Programming 11 01-05-2006 08:04 AM
trim a line schneidz Programming 2 10-07-2005 06:47 PM

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

All times are GMT -5. The time now is 10:59 AM.

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