LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 10-17-2012, 09:47 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,032

Rep: Reputation: 275Reputation: 275Reputation: 275
Unwanted blank after word deletion


Have: a file of lines containing blank-delimited words, such as ...
Code:
one two three four five
Want: a file with the second word in each line removed, such as:
Code:
one three four five
cut does this nicely:
Code:
cut -d" " -f1,3-
However, this awk ...
Code:
awk '{$2=""; print}'
... produces this ...
Code:
one  three four five
There are two blanks between "one" and "three". Is there a way to code an awk to generate output with only one blank?

Daniel B. Martin

Last edited by danielbmartin; 10-17-2012 at 09:48 AM. Reason: Cosmetic improvements
 
Old 10-17-2012, 11:38 AM   #2
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Code:
{ for (i=1; i<NF; i++)
  if (i!=2)
    printf("%s ", $i)
  printf("%s\n", $i)
}

Last edited by kabamaru; 10-17-2012 at 11:59 AM.
 
Old 10-17-2012, 11:54 AM   #3
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Changed it a bit. More compact, and removed trailing space.
 
Old 10-17-2012, 12:12 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,179

Rep: Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781
How about:
Code:
awk 'sub(" "$2,"")' file
 
1 members found this post helpful.
Old 10-17-2012, 12:16 PM   #5
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
@grail

Nice trick ! Must read about awk functions.
 
Old 10-17-2012, 12:17 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,032

Original Poster
Rep: Reputation: 275Reputation: 275Reputation: 275
Quote:
Originally Posted by grail View Post
How about:
Code:
awk 'sub(" "$2,"")' file
Sweet! Thank you! SOLVED!

Daniel B. Martin
 
Old 10-17-2012, 01:43 PM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 608

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
Hi.

grail's approach have this little problem:
Code:
$ echo 'one two three four three five' |  awk '{sub(" "$5, "")} 1'
one two four three five
When there are repeated fields, it actually removes first occurrence of the field, not necessarily a given one. In this example third field is removed instead of fifth (which contains same text as 3rd).

I myself yet to discover an elegant way to do this though.

EDIT:
Code:
$ echo 'one two three four five' |  awk '{$2=""; $0=$0; $1=$1} 1'
one three four five
which is equivalent to
Code:
$ echo 'one two three four five' |  awk '{$2=""; gsub(" +", " ")} 1'
one three four five
This way all extra spaces are lost, which may be undesirable.

BTW, just for fun, this also prints on a terminal what you want
Code:
$ echo 'one two three four five' |  awk '{$2="\b"} 1'
one three four five

Last edited by firstfire; 10-17-2012 at 02:12 PM.
 
1 members found this post helpful.
  


Reply

Tags
awk, text processing


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find/Replace shell script that replaces word with other word in text, filenames yanom Programming 8 09-12-2012 12:29 AM
have long file list; want to find word matches(package deletion purposes) lxquestions000019 Linux - Newbie 2 07-13-2009 04:28 AM
substitute few words + change all the lines starting with a specific word + put blank rahmathullakm Programming 6 01-18-2009 11:35 AM
BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line) elinenbe Programming 2 12-11-2008 01:17 PM
unwanted blank sheet between jobs alanbarnard Linux - Hardware 2 08-31-2006 03:53 AM


All times are GMT -5. The time now is 07:51 AM.

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