LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 06: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 06:16 AM.
 
Old 08-22-2012, 06:25 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 09:34 AM   #3
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
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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