ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Hello I am trying to copy a few lines from a file using sed. More specifically i want to copy lines 2-8, 10-15, 20-21 from file1 to file 2. I tried the following:
Quote:
sed '2,8p' -n file1 > file 2
This works but adding the other lines doesnt, i tried using
Quote:
sed'2,8p && 10,15p' -n file1 > file2
So im basically asking how to use a logical 'and' using sed, or how to include/exclude certain lines from a file. any suggestions?
I am still trying to use the shell version. When i use echo it works like it is supposed to, but when i output to a file it only puts the last line in it. I think that is normal as this program makes one line at a time so the file is being written several times on one line, is there an easy solution for this? thanks.
#!/bin/sh
linenum=0
while read -r line
do
linenum=$((linenum+1))
file="out1"
case "$linenum" in
[1-9]|1[2-8]|2[2-9]) echo "$line" > "$file";;
22) break;;
esac
done <"4"
Thanks to you i already got this working with sed, but im interested in the use of bash, as i want to become better at this and understand how it works.
This is getting perilously close to being beyond hard-coding the (line) numbers required, and needing a means of feeding them in. I can see a follow-up along the lines of "what if I also wanted lines ..."
Perl becomes a strong contender.
Thanks to you i already got this working with sed, but im interested in the use of bash, as i want to become better at this and understand how it works.
a lot of errors in your code. also pls indent your code next time. Is "4" your input file name? if not , change it to your input file name
Code:
#!/bin/sh
linenum=0
file="out1"
while read -r line
do
linenum=$((linenum+1))
case "$linenum" in
[1-9]|1[2-8]|2[2-9]) echo "$line" >> "$file";; #use append mode >> , not >
22) break;;
esac
done < "4"
This is getting perilously close to being beyond hard-coding the (line) numbers required, and needing a means of feeding them in. I can see a follow-up along the lines of "what if I also wanted lines ..."
bash is bash, it doesn't have things like $. (line number right??) for Perl, but incrementing counter is common in programming. nothing wrong with that. And what if i wanted other lines? ermm, add them in to the case/esac construct?
Quote:
Perl becomes a strong contender.
i would try awk first, for parsing files and for this case.
Last edited by ghostdog74; 12-15-2009 at 05:42 AM.
Sorry about the mistakes, im scripting on another machine and am copying the code by hand... Am I to understand that my mistake really was using the > thus copying only what is in the buffer at the time it is executed and overwriting the previous lines? Thanks for the reply.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.