I would like to append - at the end of each word match. But, the number of - appended should be based on the count of the match, so that the total number of characters in that line remain constant.
As shown in the example below, the total number of characters should be 6.
e.g.
ab
xyz
abcde
The above text should be replaced to:
ab----
xyz---
abcde-
You can use \= to substitute with an expression, see :h sub-replace-expression.
When the substitute string starts with \=, the remainder is interpreted as an expression.
The submatch() function can be used to obtain matched text. The whole matched text can be accessed with submatch(0). The text matched with the first pair of () with submatch(1). Likewise for further sub-matches in ().
So you can achieve it like this:
:[range]s//\=submatch(0) . repeat('-', 6-strlen(submatch(0)))/
For instance let say I have a text file:
worker1, 0001, company1
worker2, 0002, company2
worker3, 0003, company3
How would I use sed to take the first 2 characters of the first column so "wo" and remove the rest of the text and attach it to the second column so the output would look like this:
wo0001,company1
wo0002,company2
wo0003,company3
$ sed -E 's/^(..)[^,]*, ([^,]*,) /\1\2/' file
wo0001,company1
wo0002,company2
wo0003,company3
s/ begin substitution
^(..) match the first two characters at the beginning of the line, captured in a group
[^,]* match any amount of non-comma characters of the first column
, match a comma and a space character
([^,]*,) match the second field and comma captured in a group (any amount of non-comma characters followed by a comma)
match the next space character
/\1\2/ replace with the first and second capturing group
I have a comma delimited file (CSV file) test.csv as shown below.
FHEAD,1,2,3,,,,,,
FDEP,2,3,,,,,,,,
FCLS,3,,,4-5,,,,,,,
FDETL,4,5,6,7,8,
FTAIL,5,67,,,,,,
I wanted to remove the empty columns only from 2nd and 3rd row of the file i.e. were ever the records starts with FDEP and FCLS only in those rows I wanted to remove the empty columns (,,).
after removing the empty columns the same file test.csv should look like
FHEAD,1,2,3,,,,,,
FDEP,2,3
FCLS,3,4-5
FDETL,4,5,6,7,8,
FTAIL,5,67,,,,,,
How can I do this in Unix???
Here's one way to do it, using sed:
sed '/^F\(DEP\|CLS\),/ { s/,\{2,\}/,/g; s/,$// }'
We use a range of /^F\(DEP\|CLS\),/, i.e. the following command will only process lines matching ^F\(DEP\|CLS\),. This regex matches beginning-of-string, followed by F, followed by either DEP or CLS, followed by ,. In other words, we look for lines starting with FDEP, or FCLS,.
Having found such a line, we first substitute (s command) all runs (g flag, match as many times as possible) of 2 or more (\{2,\}) commas (,) in a row by a single ,. This squeezes ,,, down to a single ,.
Second, we substitute , at end-of-string by nothing. This gets rid of any trailing comma.
I'm trying to create a CSV file (Excel or LibreOffice) which contains a formula that uses multiple arguments separated by a comma. The comma is interpreted as a field separator. Is it possible to escape the comma somehow so formula requiring commas can be used?
This works as expected:
=Sum(A1:A10)
This doesn't read correctly due to use of comma in formula:
=Confidence.Norm(.01, Stdev.p(A1:A10), 10)
Imagined solutions that don't work:
=Confidence.Norm(.01 \, Stdev.p(A1:A10) \, 10)
=Confidence.Norm(.01 ',' Stdev.p(A1:A10) ',' 10)
=+"Confidence.Norm(.01 \, Stdev.p(A1:A10) \, 10)"
Try this:
"=Confidence.Norm(.01, Stdev.p(A1:A10), 10)"
I'm searching to empty out multiple columns in VIM
(not to delete but to put spaces inside).
This is my search command:
/\%2c\|\%4c\|\%>5c\%<9c
(column: 2,4,6-8)
How can I empty out these columns in vim?
:%s/\%2c\|\%4c\|\%>5c\%<9c/ /g doesn't work
/\%c is a zero-width match.
You'll need to match something like:
/\v^(.).(.).(.)...
Which will keep the values of columns 1, 3, and 5 in groups.
Then you can substitute:
:%s!\v^(.).(.).(.)...!\1 \2 \3 !
...keeping columns 1, 3, and 5 but replacing the rest of the first eight columns with spaces.