tough
tough

Reputation: 301

How to use the vim commands in script?

I would like to delete the second line of a text file. Using vim or ex any kind of text editor form script. I have came up with these commands but does not work for me.

#!/bin/sh

iconv -f Utf-16le -t utf-8  ~/Desktop/upload.csv -o ~/Desktop/finalutf.csv

vim ':2d|wq' ~/Desktop/finalutf.csv 

ex -sc '%s/\r//e|x' ~/Desktop/finalutf.csv

The script .sh is executable. First line of code works, 3rd as well but not the second one. I tried to see the documentation for the vim commands to delete the specific line, and tried it on terminal and it works (:2d) trying to use it in script seems confusing. I am new to Ubuntu as well as vim and scripts, trying to learn seems hard enough, with so much of complex commands explained leaving far away, for beginners learning vim, in its documentation.

Upvotes: 1

Views: 393

Answers (2)

Birei
Birei

Reputation: 36252

This works for me:

vim -c "2d|wq" infile

Upvotes: 4

Randy Morris
Randy Morris

Reputation: 40927

This is the sort of thing that sed is built for.

sed -i 2d ~/Desktop/finalutf.csv

However, if you must use vim you can do

vim -c "2d|wq" ~/Desktop/finalutf.csv

Upvotes: 6

Related Questions