Link to home
Start Free TrialLog in
Avatar of Jasmin shahrzad
Jasmin shahrzad

asked on

perl/awk/sed

how to from command line write line x,y,.. in to another file.
you have a file with 10000 line write line 10, 100, 150,... in another file.
how?
Avatar of arnold
arnold
Flag of United States of America image

provide a small sample of a reference file and a destiantion file and the consequence of what you want.

is this an assignment?
sed -n 10,100,150,..... < input > output
Avatar of Jasmin shahrzad
Jasmin shahrzad

ASKER

it's not working i try:
sed -n 10 file1 > file2
error:
sed: -e expression #1, char 5: missing command

i want some line from file1 to file 2 and file 2 is empty to start.
To clarify what you want is to take lines from file1 and insert them into file2
So presumably the sequence of lines extracted will depend on the length of the file....

Perl has a stat command.with seek that could get you positioned where you want to be at each step.........
dosen't matter which line step has in file1. just line ex 10,40 to file2 and becomme as line 1,2 in file2.
ASKER CERTIFIED SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sed -n '10,40 p'

tail +10 filename | tail 30

awk '{if(NR > 10){if(NR<30){print $0}}}'
there is no relation whatsoever between the accepted answer and the question.

... and that helps me notice i misread the question myself as well and picked lines from 10 to 40 rather than 10 and 40

if anyone stumbles on this thread, the correct answer is

sed -n '10 p ; 40 p' <in >out

Open in new window

The issue as the selected answer suggests was the aliasing of sed to sed -e on the asker's system/setup that adversely impacted any attempts the asker made to achieve the stated goal.

persumably the reason the asker chose ghiest's comment is once sed was unaliased, the asker was able to complete the task they were after.
i'm not discussing points or i would have objected or requested attention.
just providing a working answer for reference.

there is no way any of the suggested sed commands (including mine) would produce the desired results with or without the alias
... and i'm unsure about the alias : if i refer to the author's test : "sed -n 10 file1 > file2"
it produces the same error the author had with a pristine unaliased sed and complains about '-' being an unknown sed command with the alias
then maybe a different system producing different results but seems unlikely in this case

in case you're unsure the answer i provided is correct ( the part before the sed produces a file with line numbers so we can check what the sed picks )
$ yes | head -n 100 | grep -n . | sed -n '10 p ; 40 p'
10:y
40:y

Open in new window

mine as well, not sure whether the asker had another approach that on their system was aliased and was causing an issue; it seems -e is required so it is implicit anytime you run sed.