Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ....)
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?
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
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.