LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-04-2011, 10:29 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
AWK: delete space between two fields while keep the format for other part


Dear Experts,

I have a file like:
Code:
column1   column2 column3      column4 column5
How could I delete the space between column2 and column3(to make them into a single field), while keep the spaces between other columns?

Please pay attention that the number of spaces between other columns are all different. What I need is to keep exactly same format for other parts of the file. That means the number of spaces between other columns should not be changed.

If I simply use:
Code:
awk '$2 =$2$3 {print}' FILENAME
I can indeed delete that space but that would destroy the original format, which is not what I am expecting.

So, How to do it easily with AWK?

I would appreciate to your kind help!

Last edited by cristalp; 10-04-2011 at 10:35 AM.
 
Old 10-04-2011, 11:29 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
awk is possibly not the best tool to use here. Since it's designed for breaking things up into fields, manipulation of the delimiters between the fields is sometimes not so easy to do, particularly when you only want to affect some of them.

I'd use sed in this case, since it targets the line as a whole.
Code:
sed -r 's/(^[^[:blank:]]+[[:blank:]]+[^[:blank:]]+)[[:blank:]]+/\1/' infile.txt
This assumes that each field is separated by whitespace. It's a simple regex that matches [linestart][nospaces][whitespace][nospaces][whitespace], and prints all but the last bit of whitespace back into the line, effectively removing that part.

There may be easier ways, but it's the first solution that came to mind.

Edit: D'oh! Just after posting I realized that there is a much easier way.
Code:
sed -r 's/[[:blank:]]+//2' infile.txt
The two at the end means the substitution only affects the second match on the line. So we just tell it to match contiguous spans of whitespace, and substitute the second match with nothing.

Last edited by David the H.; 10-04-2011 at 11:40 AM. Reason: as stated
 
  


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 question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
[SOLVED] get fields using awk ashok.g Programming 9 12-09-2009 01:21 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
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Supressing Fields w/ AWK Rv5 Programming 3 10-19-2004 11:06 AM


All times are GMT -5. The time now is 05:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration