I'm wondering if there is an option to insert an "or" within sed, I need to convert a txt file to a csv file, the thing is my first column values can be up to 30 characters (minimun 1 character) if this happens the value in the next column start after the last character, otherwise space(s) are added, therefore I need to validate both cases before insert a comma, otherwise the comma might be inserted in the incorrect place.
Example:
column 1 column 2
column1-value-up-to-30-charssscolumn2-value
column1-second-value column2-second-value
column1-third-value-value column2-thrid-value-value
Expected result (comma inserted after each value, even column names)
column 1,column 2
column1-value-up-to-30-charsss,column2-value
column1-second-value,column2-second-value
column1-third-value-value,column2-thrid-value-value
Therefore, I cannot always used sed to look for and empty space (like in value one) because the comma will be inserted at the end of the value in column 2
CodePudding user response:
Tested with GNU awk:
awk 'BEGIN{FIELDWIDTHS="30 30"; OFS=","}
{
sub(/ $/, "", $1);
sub(/ $/, "", $2);
$1=$1;
print
}' file
FIELDWIDTHS: A whitespace-separated list of field widths
Output:
column 1,column 2 column1-value-up-to-30-charsss,column2-value column1-second-value,column2-second-value column1-third-value-value,column2-thrid-value-value
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
CodePudding user response:
With your given input, maybe something like this?
sed -E 's/(.{1,30})(.*)$/\1,\2/;s/[[:space:]]{2,}//' file.txt