LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2010, 03:53 PM   #1
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Rep: Reputation: 0
using cut


I have the following contents in a files

1|x|Y
2|y|X
3|d|G

I need to get the first character in the 2nd line.in this case 2

i tried
cut -d "|" -f1 filename |head -2 and its returning 1 2

how do i modify my cut statement?
 
Old 02-18-2010, 03:57 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well to directly modify your command, you'd want a tail -n 1 on the end too, but I'm sure there's a better way to do what you want. What actually is the criteria you're working with?
 
Old 02-18-2010, 04:17 PM   #3
mattca
Member
 
Registered: Jan 2009
Distribution: Slackware 14.1
Posts: 333

Rep: Reputation: 56
Are you only working with this one file? Or does your script need to work for files of arbitrary length?

Using head and tail will work in this case, but it will only work for input files with 3 lines.
 
Old 02-18-2010, 04:56 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
awk 'BEGIN{FS="|"}NR==2{print $1}' filename > newfilename
 
Old 02-18-2010, 05:06 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
And another possibility for getting the first byte from the second line:
Code:
$ sed -n '2,2p' file_here | cut -b 1
 
Old 02-19-2010, 08:16 AM   #6
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks,I used sed.
 
Old 02-19-2010, 08:31 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Based on the information we had about this file, I think the most concise solution would be
Code:
echo 2
 
  


Reply



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
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 08:55 PM
How to use Cut ZAMO Linux - General 3 09-25-2008 11:39 AM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
Cut Help metallica1973 Programming 67 12-20-2007 04:36 PM
how to cut an image ? hectorDUQUE Fedora 1 04-28-2007 09:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:06 AM.

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