How to get lines 10 to 100 from a 200 line file into a new file
Solution 1
Use sed:
sed -n -e '10,100p' input.txt > output.txt
sed -n
means don't print each line by default. -e
means execute the next argument as a sed script. 10,100p
is a sed script that means starting on line 10, until line 100 (inclusive), print (p
) that line. Then the output is saved into output.txt
.
If your file is longer than suggested, this version (suggested in the comments) will be faster:
sed -e '1,9d;100q'
That means delete lines 1-9, quit after line 100, and print the rest. For 200 lines it's not going to matter, but for 200,000 lines the first version will still look at every line even when it's never going to print them. I prefer the first version in general for being explicit, but with a long file this will be much faster — you know your data best.
Alternatively, you can use head
and tail
in combination:
tail -n +10 input.txt | head -n 91 > output.txt
This time, tail -n +10
prints out the entire file starting from line 10, and head -n 91
prints the first 91 lines of that (up to and including line 100 of the original file). It's redirected to output.txt
in the same way.
Solution 2
This should do
tail -n +10 file.txt | head -n 91 > newfile.txt
Solution 3
If you were to do this in vim
, it'd be pretty simple. Assume your file is named src
and the file you wish to move the lines to is dest
. If dest
doesn't already exist, you would create it:
touch dest
Then, open both src
and dest
in vim
(the -p
flag opens the arguments in tabs):
vim -p src dest
Jump to the tenth line; select everything from the 10th line to the 100th line; yank; switch to the tab containing dest
; paste.
10ggv101ygtp
Note: the 101
selects to the beginning of the 101st line (catches the \n
at the end of line 100).
That is obviously a little more involved process than using a command-line tool, but does have the advantage of giving you a visual selection (so you can be sure you get everything you want). However, this also seems like a fine use-case for awk
:
awk 'NR==10, NR==100' src > dest
The NR
variable allows you to pattern-match against the number of lines. Thus, the above command, extracts lines 10–100 from src
and then your shell redirects the output to dest
.
Solution 4
You can do it many ways.
An awk
solution:
$ awk 'NR<10{next};1;NR>100{exit}' file > new_file
A perl
solution:
$ perl -nle '
print if $. > 9 and $. < 101;
exit if $. > 100;
' file > new_file
Related videos on Youtube
Elias Rahme
Updated on September 18, 2022Comments
-
Elias Rahme over 1 year
Having the following html string to load in a uiwebview, i need to set the font-size as a variable value :
NSString * htmlString = [NSString stringWithFormat:@"\ <html>\ <style type='text/css'>\ hr{ height: 0;border-bottom: 2px dotted #000;}\ .Prim{color:#000000; direction:ltr;font-size:%d;font-weight:bold;}\ .Def_en{direction:ltr;font-size:%d}\ .Def_ar{direction:rtl;font-size:%d}\ </style>\ <table width='100%%'>\<tr>\<td>\ <body>\ <p style = 'font-size:%dpx;'> %@ <\p>\ </td></tr><tr><td><br><br></td></tr><tr><td align ='center'><br><img src='%@' width='60%%' \><br></td></tr></body>\ </table>\ </html>",textFontSize , authorNAme , cachePath];
This code is how i imagined it to be but it didn't work..what should i change in order of giving the font-size attribute the value of a variable textFontSize ?
Thank you in advance-
brush51 over 11 yearswhere are you presenting the webview?
-
Elias Rahme over 11 yearsthe web view loads correctly in viewDidLoad , but i have a uistepper and i took the value of it and all is normal...i just need to assign it in this css file
-
-
mikeserv almost 10 yearsThat's probably better like
sed '1,9d;100q'
. Faster anyway. -
Michael Homer almost 10 yearsFair comment. For 200 lines it doesn't matter and I prefer the more explicit version, but if it's much longer quitting (or head/tail) is definitely better. I've edited in the option anyway.
-
mikeserv almost 10 years
head | tail
will still win every time - especially because they only each do half the job but do it concurrently. -
Chad K almost 10 years@mikeserv but that's using 2 commands,
sed
alone should be faster -
Michael Homer almost 10 yearsNot for 91 lines they won't. I actually tested this - spawning two processes was about 8% slower than a single sed.
-
Chad K almost 10 years
vim
is overkill but IMHOawk
wins -
Jakob Bennemann almost 10 years@Creek, in retrospect, using a full editor for this process is probably overkill, yes. However, I think (read: dream) in
vim
anymore; I can't help it :P -
Chad K almost 10 years@MichaelHomer I clocked
real 0m0.006s
vs.real 0m0.015s
-
Chad K almost 10 yearsthere is no escape
-
mikeserv almost 10 years@Creek and Homer - in a situation where it matters - as in the file is large enough to take longer to read than spawning the processes to read it,
head | tail
will win every time. But not for 91 linews they won't, as you say, Homer. unix.stackexchange.com/q/47407/52934 -
Reihan_amn over 5 yearshow can I specify the end of file. Such as from line 10 to the end of file?