LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 03-29-2012, 10:28 AM   #1
arn2025
LQ Newbie
 
Registered: Feb 2012
Posts: 26
Blog Entries: 1

Rep: Reputation: Disabled
awk edit


this is a comma separated value text file, how do i remove the spaces before the commas

Quote:
CCN_MOD16 , 68687, 1543, 63974, 0, 658, 0, 283, , 185, 59,256777547728 ,05-FEB-12, 79.285714, 30, , ,U:726120535:3526164791328448271::840750662 , 52430,05-FEB-12, ,N, , 506, 320, , 0, , 0, 0, ,0, , , 22, , 0, 0, ,25677030212541000 ,-1 , , 8, , 0, 0, -1,256778647313 , , 192, 17.07692, 59, 25.61538, 4
CCN_MOD16 , 93222, 2905, 63225, 0, 658, 0, 287, , 0, 60,256800200028 ,05-FEB-12, 0, 0, , ,U:1009230930:3526164791328448271::141632229 , 52430,05-FEB-12, ,N, , 656, 656, , 0, , 0, 0, ,0, , , 22, , 0, 0, ,25677208352000 ,-1 , , 256, , 0, 0, -1,256783258338 , , -1, 0, 60, 0, 4
CCN_MOD16 , 68688, 1543, 62873, 0, 658, 0, 283, , 63, 20,256782834338 ,05-FEB-12, 27, 30, , ,U:1120701893:3526164791328448271::411062659 , 52430,05-FEB-12, ,N, , 576, 513, , 0, , 0, 0, ,0, , , 22, , 0, 0, ,25677030207203000 ,-1 , , 8, , 0, 0, -1,256784647114 , , 192, 5.81538, 20, 8.72308, 4
CCN_MOD16 , 66249, 2905, 61915,
0
 
Old 03-29-2012, 10:44 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,889

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
This is from The GNU Awk User's Guide:
Quote:
5.3 Output Separators
As mentioned previously, a print statement contains a list of items separated by commas. In the output, the items are normally separated by single spaces. However, this doesn't need to be the case; a single space is simply the default. Any string of characters may be used as the output field separator by setting the built-in variable OFS. The initial value of this variable is the string " "—that is, a single space.
The output from an entire print statement is called an output record. Each print statement outputs one output record, and then outputs a string called the output record separator (or ORS). The initial value of ORS is the string "\n"; i.e., a newline character. Thus, each print statement normally makes a separate line.
In order to change how output fields and records are separated, assign new values to the variables OFS and ORS. The usual place to do this is in the BEGIN rule (see BEGIN/END), so that it happens before any input is processed. It can also be done with assignments on the command line, before the names of the input files, or using the -v command-line option (see Options). The following example prints the first and second fields of each input record, separated by a semicolon, with a blank line added after each newline:
$ awk 'BEGIN { OFS = ";"; ORS = "\n\n" }
> { print $1, $2 }' BBS-list
-| aardvark;555-5553
-|
-| alpo-net;555-3412
-|
-| barfly;555-7685
...

If the value of ORS does not contain a newline, the program's output runs together on a single line.
 
Old 03-29-2012, 11:22 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: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Could you be a bit clearer about your requirements? Do you want to remove spaces only if they occur in front of a comma? What about the ones that appear after it?

If it's ok to just delete all spaces from the file, tr is all that's required.

Code:
tr -d ' ' <file
To remove only the ones before commas, a simple regex in sed can handle it easily.

Code:
sed 's/[ ]*,/,/g' file
 
  


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] Edit pattern under condition using sed or awk anath0r Programming 4 01-04-2012 03:48 PM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
Cannot edit blog comment from Android… edit box freezes as it is starting Kenny_Strawn LQ Suggestions & Feedback 3 01-05-2011 06:26 PM
help to edit Edit my bootloader's config file from kernel prompt jagdishpandey Linux - Kernel 5 07-03-2007 06:59 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM

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

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