Link to home
Start Free TrialLog in
Avatar of Jasmin shahrzad
Jasmin shahrzad

asked on

add some character at the end of line in vi

if at the end of the line is not - (dash) then dash if exist do nothing in vi
how?

i have a huge file and at the end of every line should be a dash  some line does not have a dash at the end of line. i want to make space at add dash. how can i do it in vi.
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

A sed or perl one liner is a better option for this. A simple regular expression will give you what you want.
Avatar of arnold
Can you post an example?
in short
%s/[^\-]$/\-/
The [^\-]$ checks whether the last character before the end of the line is not a dash, in which case the - is added.
Avatar of Jasmin shahrzad
Jasmin shahrzad

ASKER

Here is an example
13.7.9.229 - - [18/Dec/2016:12:00:19 +0000] "GET /B001/banking.jsp?fldsegment=ECU&fldcurrentity=B001 HTTP/1.1" 200 20360 -
14.4.9.230 - - [18/Dec/2016:12:00:20 +0000] "GET /B001/banking.jsp?fldsegment=ECU&fldcurrentity=B001 HTTP/1.1" 200 12216 -
13.4.9.229 - - [18/Dec/2016:12:00:24 +0000] "GET /B001/banking.jsp?fldsegment=ECU&fldcurrentity=B001 HTTP/1.1" 200 12216
10.7.9.230 - - [18/Dec/2016:12:00:25 +0000] "GET /B001/banking.jsp?fldsegment=ECU&fldcurrentity=B001 HTTP/1.1" 200 12216

som line has a dash at the end. i want to write dash at the end not instedet of lat char make space and dash.
Try the example posted
In vi, escape to make sure you are in command mode
:%s/[^\-]$/\-/
This will add a dash when missing.
Though as others pointed out, using sed, perl might be more efficient using the same replacement pattern since vi has to load the file in before you will ve permitted to run the option.
Saving the above in a file and then using the vi -S script filename..

The other option,
Sed -pi.bak -e 's/[^\-]$/\-/;' filename
perl -pi.bak -e 's/[^\-]$/\-/;' filename

Both will create a .bak copy, test first......
If you want to do it in vi, you do it in a 2 step process.  Just add a dash to the end of every line first, then convert all double dashes back to a single dash.

%s/$/-/g
%s/--$/-/g
Arnold:
sed: invalid option --'p'
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial