LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-26-2011, 01:22 PM   #1
zoghbi
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Rep: Reputation: Disabled
Question vi in shell script


hi everybody
I'm new to shell scripting
i need to copy some lines in a file to another existing file.
my question is how to use commands of vi in the shell script ( copy command yy or yG, substitute command :s ....)


thanks in advance
 
Old 12-26-2011, 02:54 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Hi zoghbi,

You should use the cat, head, tail, awk commands for that purpose. VI is an interactive program, not suited for scripting. Check out the manual pages.

HTH
 
Old 12-26-2011, 03:01 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Quote:
Originally Posted by zoghbi View Post
hi everybody
I'm new to shell scripting
i need to copy some lines in a file to another existing file.
my question is how to use commands of vi in the shell script ( copy command yy or yG, substitute command :s ....)


thanks in advance
If you want to do this while you are working in vi/vim, why use a shell script when you could more easily use a vi/vim script instead? If you want it done by a shell script, then presumably you can programmatically identify/modify the lines of interest with tools like grep, awk, regular expressions etc, so why involve vi/vim?
 
Old 12-27-2011, 01:47 AM   #4
zoghbi
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
dear porphyry5
thanks for your advice,
I need to use shell scripting because i have a lot of files (100 differenrs files) to effect the same function (copy the lines from the word 'system' (for example) to the end of each file(all the files contain this word), then past all this lines in another file)
i hope that you understand what i mean ( excuse me i 'm not good in English)

if you have an idea to do this work, please give us
thanks in advance
 
Old 12-27-2011, 02:23 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can use "sed -n '/system/,$p' infile >> outfile" to append to another file. The range of lines printed are from one containing "system" to the end of the file.

Put it in a loop where "infile" is a variable, and you are set.

I'd highly recommend the book "Sed & Awk" by O'Reilly. Also, gawk has a manual, "Gawk: Effective Awk Programming", which is a pretty good reference manual for awk.
 
1 members found this post helpful.
Old 12-27-2011, 03:05 AM   #6
zoghbi
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
thanks very much Mr jschiwal, your command work very well, thanks millions times.
i have another question:
if the word 'system' ( our example) is repeated twice in a file, and we would copy the lines from the second mention of this word to the end of the file, how should we do?
 
Old 12-28-2011, 01:35 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
There are two ways offhand I can think of using sed.
Code:
sed -n '/system/,$ { n
                    /system/,$p
                   }'  file >> outfile
This uses a subrange inside the main range. It will only work if you have exactly two lines with "system" in them.

A better way would be to append all lines to the buffer inside the /system/,$ range. But if such a line has another "system" in it, save the line, instead of appending, which will discard previously saved lines. On the last line, `$', append; recall the buffer; and print.

I'm not at my computer, and typing code is too painful on a small tablet, so I'll leave it as an exercise for now.
 
Old 12-28-2011, 01:44 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Another way would be to use ``tac'' to print the file out in reverse order. Pipe the output to sed.
tac file | sed '/system/{p;q}' | tac >> outfile

This will fail if the word ``system'' doesn't exist at all.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script running in another shell script and then exit dreamervlk Linux - General 3 09-16-2011 06:40 AM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration