The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to add a single line to middle of text file. progkcp Shell Programming and Scripting 2 05-12-2008 03:44 PM
Question about 'sed' heartsunny2000 Shell Programming and Scripting 4 02-21-2008 06:10 PM
How to insert text into first line of the file and middle of the file? ali hussain Shell Programming and Scripting 3 03-05-2007 05:54 AM
Number a list at end of line using 'sed' chassis Shell Programming and Scripting 5 09-13-2006 03:38 PM
awk insert character in the middle of a line mmarino Shell Programming and Scripting 3 11-18-2005 12:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-08-2006
g_jumpin g_jumpin is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 9
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 (permalink)  
Old 11-08-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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 (permalink)  
Old 11-08-2006
g_jumpin g_jumpin is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 9
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 (permalink)  
Old 11-08-2006
Manish Jha Manish Jha is offline
Registered User
  
 

Join Date: Dec 2005
Location: Boston, USA
Posts: 65
use this

echo `wc -l filename| cut -f 1 -d " "`/2 | bc
  #5 (permalink)  
Old 11-08-2006
g_jumpin g_jumpin is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 9
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 (permalink)  
Old 11-08-2006
fidodido fidodido is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 49
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0