Print file content without the first and last linesUniversal Node.js shebang?Extract middle section of lines...
Why has Speaker Pelosi been so hesitant to impeach President Trump?
Should I be an author on another PhD student's paper if I went to their meetings and gave advice?
As a team leader is it appropriate to bring in fundraiser candy?
Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?
How do we know neutrons have no charge?
Lighthouse Alternatives
Job interview by video at home and privacy concerns
Are there types of animals that can't make the trip to space? (physiologically)
Why the first octet of a MAC address always end with a binary 0?
Parent asking for money after moving out
How important is knowledge of trig identities for use in Calculus
Why do Russians sometimes spell "жирный" (fatty) as "жырный"?
Can I cast Death Ward on additional creatures without causing previous castings to end?
How is this situation not a checkmate?
What's the global, general word that stands for "center tone of a song"?
How to find places to store/land a private airplane?
Would a horse be sufficient buffer to prevent injury when falling from a great height?
Why do personal finance apps focus on outgoings rather than income
Does Bank Manager's discretion still exist in Mortgage Lending
Get the exact size of files retrieved by find output
Sending mail to the Professor for PhD, after seeing his tweet
Looking for circuit board material that can be dissolved
Why does it seem the best way to make a living is to invest in real estate?
The answer is a girl's name (my future granddaughter) - can anyone help?
Print file content without the first and last lines
Universal Node.js shebang?Extract middle section of lines of a text file?Last line is wrong when splitting a file with awksed delete all besides first and last line of many filesCompare lines and upgrade two different filesFastest & most efficient way to remove lines containing strings (strings listed in another text file)How can I copy n lines after the last 2 occurences of a string in a file?Print ranges from last occurance of a certain pattern to the first occurrence of another patternhow to find the last word in file & ignore empty spacesAdding text to a file 2 lines before the last pattern matchHow to grep and cut numbers from a file and sum them
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
Is there a simple way I can echo a file, skipping the first and last lines? I was looking at piping from head into tail, but for those it seems like I would have to know the total lines from the outset. I was also looking at split, but I don't see a way to do it with that either.
text-processing
add a comment
|
Is there a simple way I can echo a file, skipping the first and last lines? I was looking at piping from head into tail, but for those it seems like I would have to know the total lines from the outset. I was also looking at split, but I don't see a way to do it with that either.
text-processing
add a comment
|
Is there a simple way I can echo a file, skipping the first and last lines? I was looking at piping from head into tail, but for those it seems like I would have to know the total lines from the outset. I was also looking at split, but I don't see a way to do it with that either.
text-processing
Is there a simple way I can echo a file, skipping the first and last lines? I was looking at piping from head into tail, but for those it seems like I would have to know the total lines from the outset. I was also looking at split, but I don't see a way to do it with that either.
text-processing
text-processing
edited May 25 '16 at 19:40
don_crissti
55.1k18 gold badges153 silver badges180 bronze badges
55.1k18 gold badges153 silver badges180 bronze badges
asked Nov 14 '12 at 22:18
user394user394
5,51216 gold badges52 silver badges76 bronze badges
5,51216 gold badges52 silver badges76 bronze badges
add a comment
|
add a comment
|
5 Answers
5
active
oldest
votes
Just with sed, without any pipes :
sed '1d;$d' file.txt
NOTE
1mean first line
dmean delete
;is the separator for 2 commands
$mean last line
add a comment
|
You don't need to know the number of lines in advance. tail and head can take an offset from the beginning or end of the file respectively.
This pipe starts at the second line of the file (skipping the first line) and stops at the last but one (skipping the final line). To skip more than one line at the beginning or end, adjust the numbers accordingly.
tail -n +2 file.txt | head -n -1
doing it the other way round, works the same, of course:
head -n -1 file.txt | tail -n +2
I don't know why, buthead -n -1removes the first AND the last line of my.txtfile, on Ubuntu 14.04.2LTS.
– Sopalajo de Arrierez
Dec 22 '15 at 23:20
add a comment
|
Here is how to do it with awk:
awk 'NR>2 {print t} {t=$0}'
Also another way for sed:
sed '1d;x' file.txt
x is advanced sed command, it switches current line with the previous one: current goes into the buffer and previous goes to the screen and so on while sed processing stream line by line (this is why the first line will be blank).
awk solution on each step (line) puts current line into the variable and starts printing it out only after the second line is passed by. Thus, we got shitfed sequence of lines on the screen from the second to the last but one. Last line is omitted becasue the line is in the variable and should be printed only on the next step, but all steps already run out and we never see the line on the screen.
Same idea in the perl:
perl -ne 'print $t if $.>2 ; $t=$_' file.txt
$. stands for line number and $_ for current line.perl -n is shortcut for while(<..>) {..} structure and -e is for inline script.
add a comment
|
In python i would do like this.
#!/usr/bin/python3
import re
import sys
file = sys.argv[1]
with open(file, 'r') as f:
L = []
for line in f:
line = re.sub(r'n', r'', line)
L.append(line)
print('n'.join(L[1:-1]))
Paste the above code into a file and name it as script.py. Run the script against the file you want to check with.
python3 script.py /path/to/the/file
Example:
$ cat file
foo
apple
banana
bar
$ python3 script.py file
apple
banana
add a comment
|
For Mac users:
On Mac, head -n -1 doesn't work. Instead, reverse the file, chop off the first line, then reverse it back:
tail -r file.txt | tail -n +2 | tail -r
Explanation:
tail -r: reverses the order of lines in its inputtail -n +2: prints all the lines starting from the second line in its input
add a comment
|
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f55755%2fprint-file-content-without-the-first-and-last-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just with sed, without any pipes :
sed '1d;$d' file.txt
NOTE
1mean first line
dmean delete
;is the separator for 2 commands
$mean last line
add a comment
|
Just with sed, without any pipes :
sed '1d;$d' file.txt
NOTE
1mean first line
dmean delete
;is the separator for 2 commands
$mean last line
add a comment
|
Just with sed, without any pipes :
sed '1d;$d' file.txt
NOTE
1mean first line
dmean delete
;is the separator for 2 commands
$mean last line
Just with sed, without any pipes :
sed '1d;$d' file.txt
NOTE
1mean first line
dmean delete
;is the separator for 2 commands
$mean last line
edited Nov 18 '12 at 12:22
answered Nov 14 '12 at 22:24
Gilles QuenotGilles Quenot
17.4k2 gold badges42 silver badges54 bronze badges
17.4k2 gold badges42 silver badges54 bronze badges
add a comment
|
add a comment
|
You don't need to know the number of lines in advance. tail and head can take an offset from the beginning or end of the file respectively.
This pipe starts at the second line of the file (skipping the first line) and stops at the last but one (skipping the final line). To skip more than one line at the beginning or end, adjust the numbers accordingly.
tail -n +2 file.txt | head -n -1
doing it the other way round, works the same, of course:
head -n -1 file.txt | tail -n +2
I don't know why, buthead -n -1removes the first AND the last line of my.txtfile, on Ubuntu 14.04.2LTS.
– Sopalajo de Arrierez
Dec 22 '15 at 23:20
add a comment
|
You don't need to know the number of lines in advance. tail and head can take an offset from the beginning or end of the file respectively.
This pipe starts at the second line of the file (skipping the first line) and stops at the last but one (skipping the final line). To skip more than one line at the beginning or end, adjust the numbers accordingly.
tail -n +2 file.txt | head -n -1
doing it the other way round, works the same, of course:
head -n -1 file.txt | tail -n +2
I don't know why, buthead -n -1removes the first AND the last line of my.txtfile, on Ubuntu 14.04.2LTS.
– Sopalajo de Arrierez
Dec 22 '15 at 23:20
add a comment
|
You don't need to know the number of lines in advance. tail and head can take an offset from the beginning or end of the file respectively.
This pipe starts at the second line of the file (skipping the first line) and stops at the last but one (skipping the final line). To skip more than one line at the beginning or end, adjust the numbers accordingly.
tail -n +2 file.txt | head -n -1
doing it the other way round, works the same, of course:
head -n -1 file.txt | tail -n +2
You don't need to know the number of lines in advance. tail and head can take an offset from the beginning or end of the file respectively.
This pipe starts at the second line of the file (skipping the first line) and stops at the last but one (skipping the final line). To skip more than one line at the beginning or end, adjust the numbers accordingly.
tail -n +2 file.txt | head -n -1
doing it the other way round, works the same, of course:
head -n -1 file.txt | tail -n +2
edited May 13 '16 at 12:52
answered Nov 14 '12 at 22:22
Olaf DietscheOlaf Dietsche
1,0958 silver badges11 bronze badges
1,0958 silver badges11 bronze badges
I don't know why, buthead -n -1removes the first AND the last line of my.txtfile, on Ubuntu 14.04.2LTS.
– Sopalajo de Arrierez
Dec 22 '15 at 23:20
add a comment
|
I don't know why, buthead -n -1removes the first AND the last line of my.txtfile, on Ubuntu 14.04.2LTS.
– Sopalajo de Arrierez
Dec 22 '15 at 23:20
I don't know why, but
head -n -1 removes the first AND the last line of my .txt file, on Ubuntu 14.04.2LTS.– Sopalajo de Arrierez
Dec 22 '15 at 23:20
I don't know why, but
head -n -1 removes the first AND the last line of my .txt file, on Ubuntu 14.04.2LTS.– Sopalajo de Arrierez
Dec 22 '15 at 23:20
add a comment
|
Here is how to do it with awk:
awk 'NR>2 {print t} {t=$0}'
Also another way for sed:
sed '1d;x' file.txt
x is advanced sed command, it switches current line with the previous one: current goes into the buffer and previous goes to the screen and so on while sed processing stream line by line (this is why the first line will be blank).
awk solution on each step (line) puts current line into the variable and starts printing it out only after the second line is passed by. Thus, we got shitfed sequence of lines on the screen from the second to the last but one. Last line is omitted becasue the line is in the variable and should be printed only on the next step, but all steps already run out and we never see the line on the screen.
Same idea in the perl:
perl -ne 'print $t if $.>2 ; $t=$_' file.txt
$. stands for line number and $_ for current line.perl -n is shortcut for while(<..>) {..} structure and -e is for inline script.
add a comment
|
Here is how to do it with awk:
awk 'NR>2 {print t} {t=$0}'
Also another way for sed:
sed '1d;x' file.txt
x is advanced sed command, it switches current line with the previous one: current goes into the buffer and previous goes to the screen and so on while sed processing stream line by line (this is why the first line will be blank).
awk solution on each step (line) puts current line into the variable and starts printing it out only after the second line is passed by. Thus, we got shitfed sequence of lines on the screen from the second to the last but one. Last line is omitted becasue the line is in the variable and should be printed only on the next step, but all steps already run out and we never see the line on the screen.
Same idea in the perl:
perl -ne 'print $t if $.>2 ; $t=$_' file.txt
$. stands for line number and $_ for current line.perl -n is shortcut for while(<..>) {..} structure and -e is for inline script.
add a comment
|
Here is how to do it with awk:
awk 'NR>2 {print t} {t=$0}'
Also another way for sed:
sed '1d;x' file.txt
x is advanced sed command, it switches current line with the previous one: current goes into the buffer and previous goes to the screen and so on while sed processing stream line by line (this is why the first line will be blank).
awk solution on each step (line) puts current line into the variable and starts printing it out only after the second line is passed by. Thus, we got shitfed sequence of lines on the screen from the second to the last but one. Last line is omitted becasue the line is in the variable and should be printed only on the next step, but all steps already run out and we never see the line on the screen.
Same idea in the perl:
perl -ne 'print $t if $.>2 ; $t=$_' file.txt
$. stands for line number and $_ for current line.perl -n is shortcut for while(<..>) {..} structure and -e is for inline script.
Here is how to do it with awk:
awk 'NR>2 {print t} {t=$0}'
Also another way for sed:
sed '1d;x' file.txt
x is advanced sed command, it switches current line with the previous one: current goes into the buffer and previous goes to the screen and so on while sed processing stream line by line (this is why the first line will be blank).
awk solution on each step (line) puts current line into the variable and starts printing it out only after the second line is passed by. Thus, we got shitfed sequence of lines on the screen from the second to the last but one. Last line is omitted becasue the line is in the variable and should be printed only on the next step, but all steps already run out and we never see the line on the screen.
Same idea in the perl:
perl -ne 'print $t if $.>2 ; $t=$_' file.txt
$. stands for line number and $_ for current line.perl -n is shortcut for while(<..>) {..} structure and -e is for inline script.
answered Nov 18 '14 at 13:02
rookrook
5371 gold badge8 silver badges15 bronze badges
5371 gold badge8 silver badges15 bronze badges
add a comment
|
add a comment
|
In python i would do like this.
#!/usr/bin/python3
import re
import sys
file = sys.argv[1]
with open(file, 'r') as f:
L = []
for line in f:
line = re.sub(r'n', r'', line)
L.append(line)
print('n'.join(L[1:-1]))
Paste the above code into a file and name it as script.py. Run the script against the file you want to check with.
python3 script.py /path/to/the/file
Example:
$ cat file
foo
apple
banana
bar
$ python3 script.py file
apple
banana
add a comment
|
In python i would do like this.
#!/usr/bin/python3
import re
import sys
file = sys.argv[1]
with open(file, 'r') as f:
L = []
for line in f:
line = re.sub(r'n', r'', line)
L.append(line)
print('n'.join(L[1:-1]))
Paste the above code into a file and name it as script.py. Run the script against the file you want to check with.
python3 script.py /path/to/the/file
Example:
$ cat file
foo
apple
banana
bar
$ python3 script.py file
apple
banana
add a comment
|
In python i would do like this.
#!/usr/bin/python3
import re
import sys
file = sys.argv[1]
with open(file, 'r') as f:
L = []
for line in f:
line = re.sub(r'n', r'', line)
L.append(line)
print('n'.join(L[1:-1]))
Paste the above code into a file and name it as script.py. Run the script against the file you want to check with.
python3 script.py /path/to/the/file
Example:
$ cat file
foo
apple
banana
bar
$ python3 script.py file
apple
banana
In python i would do like this.
#!/usr/bin/python3
import re
import sys
file = sys.argv[1]
with open(file, 'r') as f:
L = []
for line in f:
line = re.sub(r'n', r'', line)
L.append(line)
print('n'.join(L[1:-1]))
Paste the above code into a file and name it as script.py. Run the script against the file you want to check with.
python3 script.py /path/to/the/file
Example:
$ cat file
foo
apple
banana
bar
$ python3 script.py file
apple
banana
answered Nov 18 '14 at 15:40
Avinash RajAvinash Raj
2,7023 gold badges12 silver badges28 bronze badges
2,7023 gold badges12 silver badges28 bronze badges
add a comment
|
add a comment
|
For Mac users:
On Mac, head -n -1 doesn't work. Instead, reverse the file, chop off the first line, then reverse it back:
tail -r file.txt | tail -n +2 | tail -r
Explanation:
tail -r: reverses the order of lines in its inputtail -n +2: prints all the lines starting from the second line in its input
add a comment
|
For Mac users:
On Mac, head -n -1 doesn't work. Instead, reverse the file, chop off the first line, then reverse it back:
tail -r file.txt | tail -n +2 | tail -r
Explanation:
tail -r: reverses the order of lines in its inputtail -n +2: prints all the lines starting from the second line in its input
add a comment
|
For Mac users:
On Mac, head -n -1 doesn't work. Instead, reverse the file, chop off the first line, then reverse it back:
tail -r file.txt | tail -n +2 | tail -r
Explanation:
tail -r: reverses the order of lines in its inputtail -n +2: prints all the lines starting from the second line in its input
For Mac users:
On Mac, head -n -1 doesn't work. Instead, reverse the file, chop off the first line, then reverse it back:
tail -r file.txt | tail -n +2 | tail -r
Explanation:
tail -r: reverses the order of lines in its inputtail -n +2: prints all the lines starting from the second line in its input
edited 1 hour ago
user664833
1324 bronze badges
1324 bronze badges
answered Oct 16 '16 at 4:01
Sarfraaz AhmedSarfraaz Ahmed
1212 bronze badges
1212 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f55755%2fprint-file-content-without-the-first-and-last-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown