LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   question about sed (https://www.linuxquestions.org/questions/programming-9/question-about-sed-775561/)

Magil 12-14-2009 09:02 AM

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:

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?

druuna 12-14-2009 09:08 AM

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.

Magil 12-14-2009 09:17 AM

Thanks a lot works like a charm.

ghostdog74 12-14-2009 09:25 AM

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
or with just the shell
Code:

#!/bin/bash
linenum=0
while read -r line
do
  linenum=$((linenum+1))
  case "$linenum" in
    [2-8]|1[0-5]|2[01]) echo "$line";;
    22) break;;
  esac   
done <"file"


Magil 12-14-2009 09:41 AM

ah, using awk does look a bit simpler as i have no clue about the sed variables, thanks!

Magil 12-14-2009 10:10 AM

Quote:

Originally Posted by ghostdog74 (Post 3791005)
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
or with just the shell
Code:

#!/bin/bash
linenum=0
while read -r line
do
  linenum=$((linenum+1))
  case "$linenum" in
    [2-8]|1[0-5]|2[01]) echo "$line";;
    22) break;;
  esac   
done <"file"


One more question though, in the shell, how can i declare the input file, it looks like im only writing an output file but no input? thanks.

ghostdog74 12-14-2009 07:50 PM

did you see the <"file" ?? to declare variables in shell, do it as normal --> filename="file". then use it as $filename

Magil 12-15-2009 05:02 AM

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.

ghostdog74 12-15-2009 05:16 AM

show your script then, how you output to file.

Magil 12-15-2009 05:27 AM

Quote:

#!/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.

syg00 12-15-2009 05:28 AM

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.

ghostdog74 12-15-2009 05:37 AM

Quote:

Originally Posted by Magil (Post 3792115)
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"


ghostdog74 12-15-2009 05:41 AM

Quote:

Originally Posted by syg00 (Post 3792116)
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.

Magil 12-15-2009 05:51 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.


All times are GMT -5. The time now is 09:20 AM.