LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-22-2012, 07:15 AM   #1
anshaa
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Rep: Reputation: Disabled
Removing extra unwanted spaces


hi,

i need to remove the extra spaces in the filed.

Sample:

abc|bd |bkd123 .. 1space
abc|badf |bakdsf123 .. 1space
abc|bqe |bakuowe .. 2space

output:

abc|bd|bkd123
abc|badf|bakdsf123
abc|bqe|bakuowe

i used the following command,
nawk -F\~ 'OFS=FS { gsub(" ", "", $3) }1'
but it doesnt remove if it has more than one space.

Please help

Last edited by anshaa; 08-22-2012 at 07:16 AM.
 
Old 08-22-2012, 07:25 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Here's a sed solution:
Code:
sed 's/ //g' infile
If that does what you want/need add the -i flag to make the changes in-place or, if your sed version does not support -i, redirect to another file:
Code:
# 1
sed -i 's/ //g' infile

# 2
sed 's/ //g' infile > outfile
 
Old 08-22-2012, 10:34 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.

Also, please explain in more detail. Do you just want to remove all spaces in the file, or only certain ones?

The most common tool to use for manipulating files on a character level is tr.

Code:
tr -d ' ' <infile >outfile
Quote:
i used the following command,
nawk -F\~ 'OFS=FS { gsub(" ", "", $3) }1'
but it doesnt remove if it has more than one space.
Your example text has "|" as the delimiter, but your command is using "~". Also, this will only removes spaces in field number 3. Is that what you want? Your sample text doesn't have any spaces there. Just leave the last parameter off of the gsub function to have it operate on the whole line. Finally, the first parameter can be a regex. So to remove all spaces with awk:

Code:
nawk '{ gsub( /[ ]+/ , "" ) } 1'  infile >outfile
 
  


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
[SOLVED] ubuntu : delete the extra spaces Xanios Linux - Newbie 13 07-29-2010 12:15 AM
Extra unwanted links to unmounted NTFS partitions in dolphin 'places' bar tomgibson Linux - Newbie 2 02-18-2009 09:35 AM
[SOLVED] Remove extra spaces in a line vikas027 Programming 11 10-11-2008 09:20 AM
Html: Extra Spaces on Top mikeshn Programming 1 07-05-2003 04:40 PM
Removing unwanted RH 7.2 packages!! CyberDrake Linux - Distributions 1 02-08-2002 06:33 AM


All times are GMT -5. The time now is 07:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration