You are not logged in.

#1 2010-07-06 01:28:46

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

[SOLVED](use vim instead) Using ex's -c argument

Say I have a file that looks like this

*background:
rgb:FF/FF/FF
*color0:
rgb:0/0/0
*color8:
rgb:68/68/68
*color1:
rgb:B2/18/18
*color9:
rgb:FF/54/54
*color2:
rgb:18/B2/18
*color10:
rgb:54/FF/54
*color3:
rgb:B2/68/18
*color11:
rgb:FF/FF/54
*color4:
rgb:18/18/B2
*color12:
rgb:54/54/FF
*color5:
rgb:B2/18/B2
*color13:
rgb:FF/54/FF
*color6:
rgb:18/B2/B2
*color14:
rgb:54/FF/FF
*color7:
rgb:B2/B2/B2
*color15:
rgb:FF/FF/FF
*foreground:
rgb:0/0/0

I want to put all the lines that start with rgb at the end of the line before them. Sounds simple, right? I can do this if I open up ex manually and type in three commands, but I want to be able to just type one command into the terminal/use this in a script. I looked at the man page and thought -c would solve all my problems, but I can't get it to work. I'm trying

ex -c "g/\*/j
w
q" file

The only ways I can get it to work is are practically the same thing, run ex in script mode and give it a script file/pipe the commands into it

ex -s file < ex_script
# or
echo "g/\*/j
w
q" | ex -s file

What am I doing wrong? Also, how can I put multiple ex commands on one line? Is that possible?
I know how to do this with sed also, but I'd rather do it with ex because I think it'll be less obtuse.

Last edited by jac (2010-07-06 10:59:44)

Offline

#2 2010-07-06 01:41:16

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,294

Re: [SOLVED](use vim instead) Using ex's -c argument

I have never masted their use (so I cannot provide any examples) but I believe this is exactly what sed, and maybe awk are for.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2010-07-06 01:48:59

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: [SOLVED](use vim instead) Using ex's -c argument

As I said, I can do it with sed, but sed is not designed to work with multiple lines. To do it in sed you need to read the next line, and then change the \n character into nothing, like this:

sed '/\*/ {
N
s/\n//
}' file

Not as pretty as what I'm hoping to get out of ex, plus I just want to know how to use that darn argument, haha. I can only get really simple things to work using -c or +, like 'q', not very useful.

Offline

#4 2010-07-06 09:57:36

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED](use vim instead) Using ex's -c argument

vim does that by multiple -c arguments. I don't have ex but I guess it's the same
vim -e -c 'g/\*/j' -c wq file.txt
In this case all the -e for ex is good for is stopping it from trying to make the curses window.

Offline

#5 2010-07-06 10:59:10

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: [SOLVED](use vim instead) Using ex's -c argument

Oh, I was mistaken in thinking vim had its own ex binary/wrapper. I tried the multiple -c commands with ex yesterday, and it only executed the last one. This does work with vim as you have described.

This solves it for me because I thought I was using the ex-thing from vim anyway. Looking at the man page for vim, it has a limit of 10 -c/+ switches, so I'm guessing this is added functionality of vim and that is why it doesn't work with standard ex.

Thank you, Procyon!

Offline

Board footer

Powered by FluxBB