LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk edit (https://www.linuxquestions.org/questions/linux-newbie-8/awk-edit-937100/)

arn2025 03-29-2012 10:28 AM

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

bigrigdriver 03-29-2012 10:44 AM

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.

David the H. 03-29-2012 11:22 AM

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


All times are GMT -5. The time now is 12:14 AM.