|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
[bash] Shell scripting VI commands
Hi there,
I have the following problem. I need to copy across a tab-delimited text file from one server to another, strip the first two lines (which are a header of information) and then import into mysql. I want this all this done in a shell script. I have solved the first issue (using ssh-keygen) and can copy to and from different servers. However, I want (in my shell script) to open vi and delete two lines (so, `dd` twice). Can anyone help me with scripting vi commands? Every time I use Google with the search words "vi shell scripting" I get literally thousands of results of the like: "when I shell script I use vi" etc etc - no use at all. Any help appreciated - even links to helpful websites . My shell is bash and my OS is Linux Red Hat.Cheers, - Tatlar |
|
#2
|
||||
|
||||
|
You can do it without using vi. I'm using wc, cut, dc and tail to do this. Promise me that you won't laugh at this approach
.Code:
#!/bin/bash # First compute how many lines we have in the page lines=`wc -l $1 | cut -c1-8` # Next subtract 2 from that number (using dc to do the calculation) lines2=`dc -e "$lines 2 - n"` # Now tail out the last n-2 lines to a new file tail -$lines2 $1 > $1.out I'm assuming that the filename to process is passed as an argument to the script. e.g. ./clipit.sh file_to_process.txt HTH ![]()
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month Looking for a perl job with kick-*** programmers in a well-known NASDAQ listed tech company with branches in the US and Europe? We're hiring. PM me for details. Requirements |
|
#3
|
||||
|
||||
|
Thanks for the help - I won't laugh if it solves my problem :P
- T |
|
#4
|
||||
|
||||
|
hey Scorpions4ever,
thanks buddy! - that worked like a charm and has made my life infinitely better i owe you one. - tatlar |
|
#5
|
||||
|
||||
|
another way...
tatlar...
Another way to get the lines count would be just cat filename | wc -l I'm not sure why Scorpions4ever used cut, since wc -l gives you a one-field answer, but if you needed to get *just* the arithmetic value, you could use this: lines=$((`cat filename | wc -l`)) The rest of his post is excellent; I wasn't laughing at all. ;) By the way, I've wondered a few times how to get vi commands executed in a shell script. if you find out, please let me know. Scorpions4ever, your .sig is sweeeeet. That's awesome!
__________________
Mother says my .sig can beat up your .sig. |
|
#6
|
||||
|
||||
|
>> I'm not sure why Scorpions4ever used cut, since wc -l gives you a one-field answer
Executing wc -l filename on my RedHat Linux system outputs both the line count and the filename (i.e. two fields). That's why I used cut to chop out the line count field from the returned line. HTH ![]() |
|
#7
|
||||
|
||||
|
the many, many facets of wc -l
heh...
My first response was "WHAT?!??! That's so weird, because I've never heard of that before!" But then I paid closer attention to your syntax. I hadn't realized that I've always used wc in the secondary position, never as primary: cat .profile | wc -l I just tested it by executing this: wc -l .profile ...and it was just like you said: 48 .profile ...on Open BSD ...and Slackware ...and AIX ...and Solaris ...and Red Hat so, it looks like I've proven once again (I've done it a lot) that you can post a comment THINKING you're making a correction, but really you're just ignorant. I was talking just today with someone about learning constantly. Hmmmm. Wonder who that could've been.... ;) -zedmelon |
|
#8
|
||||
|
||||
|
clarification
to make things painfully apparent, since at the moment, they're painfully *not* apparent, I, zedmelon, was the ignorant party I referenced in the last paragraph saying something like, "...you can be ignorant, yet think you're making a good point..."
Heh... no point in starting a flame war if I'm aiming the first flaming arrow at myself but simply miss. ;) -zedmelon |
|
#9
|
|||
|
|||
|
One simple way that I have used vim in shell scripts was as follows (ok, I could have used sed/awk/etc, but I really like vi)
(ok, I donīt know how to use the others so I've used vi) I was trying to change newlines to spaces and what I have done: vim '+%s/\n/ /g' '+wq' file2edit.txt I hope that could be useful Regards, Ailton Oliveira |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > [bash] Shell scripting VI commands |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|