LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-29-2007, 11:02 AM   #1
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Rep: Reputation: 17
Unhappy Command to separate 2nd line's 3rd word from a text file


HI,
Please suggest me the command in linux to separate second line's 3rd word from a text file.
 
Old 11-29-2007, 11:09 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
awk would be ideal.
 
Old 11-29-2007, 02:33 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
cat filename |sed -n '2p'|cut -d " " -nf 3
Assumes that words are delimited by exactly one space. If more than one space, add another sed to replace N spaces with one.

Not tested:

Code:
sed '/ */ /g'
 
Old 11-29-2007, 02:45 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Code:
awk 'NR==2 { print $3 ; exit }'
 
Old 11-29-2007, 09:03 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by matthewg42 View Post
Code:
awk 'NR==2 { print $3 ; exit }'
I'll match that and raise you one.... Next thing--someone will pop in with the PERL solution.
 
Old 11-29-2007, 09:53 PM   #6
Slokunshialgo
Member
 
Registered: Jan 2006
Distribution: Ubuntu 9.04, Fedora 10
Posts: 76

Rep: Reputation: 17
Want some Perl?

Code:
#!/usr/bin/perl

$file = `cat $ARGV[0]`;
@lines = split(/\n/, $file);
$line = @lines[1];
$line =~ s/^([^ ]+[ ]+[^ ]+[ ]+)([^ ]+)([ ]+.*)/$2/;
print $line;
No idea if it'll work or not, and I know it's probably highly inefficient, but it's Perl.

EDIT: Somebody pointed out I got the wrong word

Last edited by Slokunshialgo; 11-29-2007 at 10:08 PM.
 
Old 11-29-2007, 11:31 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by pixellany View Post
Code:
cat filename |sed -n '2p'|cut -d " " -nf 3
Assumes that words are delimited by exactly one space. If more than one space, add another sed to replace N spaces with one.

Not tested:

Code:
sed '/ */ /g'
Please stop doing that!
Use the form:
"sed -n '2p' filename |"

instead of
"cat filename | sed '/ */ /g' |"

There is absolutely no reason to use cat before sed or awk unless you want to add line numbers, or are using zcat with a compressed file.

IMHO, awk would be the best tool:
awk 'NR==2{ print $3 }' filename

No cat, only one program used, and extra white space is handled automatically.

Using perl would be overkill given the size of the perl shell. Sort of like going to the corner store in your Sherman tank instead of your sedan.

---

Yikes, looking back on the previous messages, I forgot the exit. Shame on me!

Last edited by jschiwal; 11-29-2007 at 11:38 PM.
 
Old 11-29-2007, 11:36 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I'm trying to work on my reputation as a consistent offender in the unnecessary use of cat.....

Seriously, I do that because it's sometimes easier to explain what is happening. If I'm actually writing a script, I will be more efficient---I promise!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM
how to change a word in a text file EXnihilo Linux - General 11 09-11-2006 08:07 AM
Mandrake installation problems with 2nd and 3rd CD SidA Mandriva 2 03-21-2004 05:48 AM
Where to mount 2nd and 3rd disks vrillusions Linux - Hardware 3 11-29-2003 05:13 PM
How 2 find a duplicate word in a text file cowardnewbie Programming 1 09-16-2001 11:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration