How to 'sed' the middle line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to 'sed' the middle line?
# 1  
Old 11-08-2006
How to 'sed' the middle line?

Hi,

If I have 90 lines and I want to print the 45th line, is there a quick sed command for this?

thx.
# 2  
Old 11-08-2006
sed2liners
Code:
# print line number 52
 sed -n '52p'                 # method 1
 sed '52!d'                   # method 2
 sed '52q;d'                  # method 3, efficient on large files

# 3  
Old 11-08-2006
Thanks, but would if I did not know the middle line number?

Is there a formula to figure out the middle line number?

Something like (wc -l) divided by 2 ?

thx.
# 4  
Old 11-08-2006
use this

echo `wc -l filename| cut -f 1 -d " "`/2 | bc
# 5  
Old 11-08-2006
echo `wc -l filename| cut -f 1 -d " "`/2 | bc
syntax error on line 1, teletype

Thanks for the replies, but I can't get it to work. The file is a list of names:

Frank
Sally
Sam
Julie
Joe
Jay
Ralph
John
Sally
Fred
Sue

word count(lines) is 11

I need to print the middle line which is 'Jay' which is the middle (6th) line.

thx.
# 6  
Old 11-08-2006
Don't know which would be faster, but..
Code:
# cat names
Frank
Sally
Sam
Julie
Joe
Jay
Ralph
John
Sally
Fred
Sue
# awk '{ lines[NR]=$0; } END { print lines[int(NR/2)+1] }' names
Jay
#

seems to do it.
# 7  
Old 11-08-2006
Code:
half=$(( $( wc -l < myFile.txt) / 2 )); echo $half

.... the rest can be done with the original post.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

2. UNIX for Dummies Questions & Answers

Using sed to remove spaces from middle of the line

Hi, I need to correct href portion of the lines to edit out spaces from the line starting with position "<a href=" and ending at "target=" Below are 2 examples of extra space added by the server: <td width=251 colspan=9 rowspan=22> <font size=2 face="courier"><tt><style>{font:7pt Courier ... (4 Replies)
Discussion started by: friedmi
4 Replies

3. Shell Programming and Scripting

Search for a pattern that is in the middle of the line.

Hello, How do I use sed to search for a pattern in the middle of a line and delete the whole line. The command I have is as below. sed -i '/soft nofile/ d' /etc/security/limits.conf I have some *'s and spaces in front of the string. With the above command I'm not able to delete it. Any... (3 Replies)
Discussion started by: jawaugh
3 Replies

4. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

5. Shell Programming and Scripting

Find the middle character from a string using sed

Input: qwertmyuiop It should print "m". What's the command to do this with sed? (6 Replies)
Discussion started by: cola
6 Replies

6. UNIX for Dummies Questions & Answers

sed replacing in vi, / characters in the middle

I tried to replace the following in vi: old: 'e/thesis/pp/zones/zones' new: 'd/so162/fix/pp' For that, I used: :%s/e/thesis/pp/zones/zones/d/so162/fix/pp/g but doesn't work, a trailing character error message appeared. How can I get it? Thanks in advance (3 Replies)
Discussion started by: Gery
3 Replies

7. UNIX for Dummies Questions & Answers

VIM: replace a character in the middle of line

exmaple, i need to replace the number "5" from all lines below with "X"? What is the useful vim command that i can apply for.. ddpadsgg506xghssuyj ddpadsgag546xghssuys ddsadsgaag596xghssuy_te ddsadsgag506xghssuy_pe ddsadsgagc526xghssuys ddsads506ighssuys ddsadsgag506pghssuyk (1 Reply)
Discussion started by: 793589
1 Replies

8. Shell Programming and Scripting

How do I insert a line in the middle of a file in BASH?

I have tried sed '/6/a text_to_inserted' file > newfile but this inserts test_to_insert at random places in file and i want it in specific location, which is line 6. can anyone help.... (6 Replies)
Discussion started by: phenom
6 Replies

9. Shell Programming and Scripting

awk insert character in the middle of a line

I'm trying to insert a single character at position 11 in everyline of a file. My input file looks like this: 456781 ~Y~12345 456782 ~N~12300 and I want my output to look like this: 45678~1 ~Y~12345 45678~2 ~N~12300 I tried the following awk code, but it's not working:... (3 Replies)
Discussion started by: mmarino
3 Replies
Login or Register to Ask a Question