UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 27th, 2003, 05:36 PM
tatlar's Avatar
tatlar tatlar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Surfside
Posts: 15 tatlar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 47 sec
Reputation Power: 0
[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

Reply With Quote
  #2  
Old July 27th, 2003, 06:11 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,536 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 6 h 51 m 7 sec
Reputation Power: 876
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

Reply With Quote
  #3  
Old July 28th, 2003, 12:18 PM
tatlar's Avatar
tatlar tatlar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Surfside
Posts: 15 tatlar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 47 sec
Reputation Power: 0
Thanks for the help - I won't laugh if it solves my problem :P

- T

Reply With Quote
  #4  
Old July 28th, 2003, 01:31 PM
tatlar's Avatar
tatlar tatlar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Surfside
Posts: 15 tatlar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 47 sec
Reputation Power: 0
Talking

hey Scorpions4ever,

thanks buddy! - that worked like a charm and has made my life infinitely better

i owe you one.

- tatlar

Reply With Quote
  #5  
Old August 6th, 2003, 01:31 AM
zedmelon's Avatar
zedmelon zedmelon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: under a rock
Posts: 54 zedmelon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 20 m 42 sec
Reputation Power: 6
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.

Reply With Quote
  #6  
Old August 6th, 2003, 03:53 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,536 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 6 h 51 m 7 sec
Reputation Power: 876
>> 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

Reply With Quote
  #7  
Old August 6th, 2003, 04:34 PM
zedmelon's Avatar
zedmelon zedmelon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: under a rock
Posts: 54 zedmelon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 20 m 42 sec
Reputation Power: 6
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

Reply With Quote
  #8  
Old August 6th, 2003, 04:38 PM
zedmelon's Avatar
zedmelon zedmelon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: under a rock
Posts: 54 zedmelon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 20 m 42 sec
Reputation Power: 6
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

Reply With Quote
  #9  
Old November 4th, 2005, 11:33 AM
oailton oailton is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 1 oailton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 46 sec
Reputation Power: 0
Cool Scripting with vi

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > [bash] Shell scripting VI commands


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway