question about sed
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:
Quote:
|
Hi,
You can "string" sed statements like this: sed -ne '2,8p' -e '10,15p' -e '20,21p' infile The -e options being the key to it all. Hope this helps. |
Thanks a lot works like a charm.
|
better to put "q" to quit after line 21.
an alternative with awk Code:
awk 'NR~/^([2-8]|1[0-5]|2[01])$/{print}NR>21{exit}' file Code:
#!/bin/bash |
ah, using awk does look a bit simpler as i have no clue about the sed variables, thanks!
|
Quote:
|
did you see the <"file" ?? to declare variables in shell, do it as normal --> filename="file". then use it as $filename
|
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.
|
show your script then, how you output to file.
|
Quote:
|
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. |
Quote:
Code:
#!/bin/sh |
Quote:
Quote:
|
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.
|
All times are GMT -5. The time now is 09:20 AM. |