# Anyone here know AWK or SED?

**URL:** <https://boards.straightdope.com/t/anyone-here-know-awk-or-sed/51541>\
**Category:** Factual Questions\
**Created:** [January 24, 2001, 1:29pm UTC](https://boards.straightdope.com/t/anyone-here-know-awk-or-sed/51541 "2001-01-24T13:29:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaotic\_Newtral](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Kaotic\_Newtral](https://boards.straightdope.com/u/Kaotic_Newtral)\
**Post date:** [January 24, 2001, 1:29pm UTC](https://boards.straightdope.com/t/anyone-here-know-awk-or-sed/51541/1 "2001-01-24T13:29:35Z")

</div>

I’m having a problem today at work remembering awk.

Say I’ve got a file with the lines:

This line doesn’t count;  
The quick brown  
fox jumped  
over the lazy dog;  
This line doesn’t count either;

how can I get the middle three lines from an awk script with just one command? I’ve done this before but I can’t remember the syntax and don’t have my awk book available.  
Any help?

---

<div class="post-metadata">

**Author:** ![Perderabo](https://avatars.discourse-cdn.com/v4/letter/p/a183cd/32.png) [@Perderabo](https://boards.straightdope.com/u/Perderabo)\
**Post date:** [January 24, 2001, 2:14pm UTC](https://boards.straightdope.com/t/anyone-here-know-awk-or-sed/51541/2 "2001-01-24T14:14:44Z")

</div>

If I understand what you’re asking I would use sed instead of awk:

sed -e 1d -e 4q \< file

---

<div class="post-metadata">

**Author:** ![douglips](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@douglips](https://boards.straightdope.com/u/douglips)\
**Post date:** [January 24, 2001, 5:51pm UTC](https://boards.straightdope.com/t/anyone-here-know-awk-or-sed/51541/3 "2001-01-24T17:51:51Z")

</div>

You don’t need sed or awk. How about:

```auto

cat foo.txt | tail -4 | head -3

```

Or, what Perderabo said.

If you need to use sed or awk I can’t help you as I’ve forgotten both since I started using perl 4-5 years ago. One day I might get in trouble if I’m on a system without perl, but hopefully ‘man awk’ will be available and refresh my brain.

In perl, a generic program to print all but the first and last lines of a file of any size might look like

```auto

@a = <>;
shift @a;
pop @a;
print @a;

```

You could one-line this with

```auto

perl -e "@a=<>;shift @a;pop @a;print @a;" foo.txt

```

Of course, it’s perl, so there is more than one way to do it.

---

<div class="post-metadata">

**Author:** ![Bobort](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@Bobort](https://boards.straightdope.com/u/Bobort)\
**Post date:** [January 24, 2001, 9:01pm UTC](https://boards.straightdope.com/t/anyone-here-know-awk-or-sed/51541/4 "2001-01-24T21:01:10Z")

</div>

If you just want the middle 3 lines of 5 lines of text, this will do:

```auto

NR == 2 || NR == 3 || NR == 4 {print}

```
