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
reading lines in pairs from file in ksh ytokar Shell Programming and Scripting 4 02-08-2008 02:50 PM
skip reading certain lines in a file paulsew UNIX for Dummies Questions & Answers 5 03-26-2007 04:07 AM
Reading multiple lines as single braindrain Shell Programming and Scripting 9 10-18-2006 11:16 AM
Reading lines within vi editor asriva26 Shell Programming and Scripting 2 08-30-2006 10:47 AM
Reading lines within a Unix file. mariner UNIX for Advanced & Expert Users 5 11-18-2003 01:26 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 05-17-2008
c0mrade c0mrade is offline
Registered User
  
 

Join Date: May 2008
Posts: 35
reading lines

Hi all,

I have a file whose content I need to read and get the 3rd line .. ex:


Code:
one
two
three

What I need is to read a content of third line of this file and print first three characters ... I could print first three characters with "head -c 3" if I could get the content of the third line ... that is what I struggle with.. please help

thank you in advance
  #2 (permalink)  
Old 05-17-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 167

Code:
$ cat test.txt
one
two
three

$ sed '3!d' test.txt | cut -c1-3
thr

//Jadu
  #3 (permalink)  
Old 05-17-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Might as well do it all in sed.


Code:
sed -n '3s/\(...\).*/\1/p' test.txt

  #4 (permalink)  
Old 05-17-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 167
Era's solution helped me to find two more similar ways . Thank you Era.


Code:
$ sed -n '3 s/\(.\{3\}\).*/\1/p' test.txt
thr

$ awk 'NR==3 {print substr($0,1,3)}' test.txt
thr

//Jadu
  #5 (permalink)  
Old 05-17-2008
c0mrade c0mrade is offline
Registered User
  
 

Join Date: May 2008
Posts: 35
thank you guys..
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 01:24 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