LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-02-2011, 08:44 AM   #1
Passmossis
LQ Newbie
 
Registered: Jul 2011
Location: USA
Distribution: RHEL 6.5
Posts: 28

Rep: Reputation: 2
Scripting: catching first line into variable


I am attempting to capture only the first return from a multiple line return into a variable.

Command results:
Code:
TVE-2001-1111
TVE-2001-1111
TVE-2001-1111
TVE-2001-1111
Variable results after capture:
Code:
TVE-2001-1111 TVE-2001-1111 TVE-2001-1111 TVE-2001-1111
What I need:
Code:
TVE-2001-1111
I've tried using awk on the variable in the script by
Code:
cat $VarName | awk '{$print %1}'
But alas it's not that easy. I'm sure there is something simple I am missing. Any help would be much appreciated.
 
Old 11-02-2011, 08:50 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Use 'head':
Code:
variable=$(someCommand | head -n1)
 
1 members found this post helpful.
Old 11-02-2011, 08:52 AM   #3
Passmossis
LQ Newbie
 
Registered: Jul 2011
Location: USA
Distribution: RHEL 6.5
Posts: 28

Original Poster
Rep: Reputation: 2
See, I just knew there was something that would fit exactly what I needed. Much appreciated.

Setting topic as solved.
 
Old 11-03-2011, 05:22 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Even easier:

Code:
read variable <file
read takes in one line (or substring as defined by -d or -n) each time it's run. Used in a loop it will cycle through the file, but since we're only launching it once, you get only the first line.

You can also use multiple variables or -a to capture the individual words in the line.

More on using read here:
http://mywiki.wooledge.org/BashFAQ/001


Edit: To capture the output of a command, use a process substitution.

Code:
read variable < <( commands )

Last edited by David the H.; 11-03-2011 at 05:27 AM.
 
1 members found this post helpful.
Old 11-03-2011, 06:57 AM   #5
Passmossis
LQ Newbie
 
Registered: Jul 2011
Location: USA
Distribution: RHEL 6.5
Posts: 28

Original Poster
Rep: Reputation: 2
I'm sure I'll be able to put that to you as well, thanks David the H!
 
  


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
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
bash catching the function returning value into a variable patolfo Linux - General 6 03-19-2010 11:58 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
Shell scripting : how to name a variable with the value of another variable JimOrJoe Programming 9 06-15-2008 09:36 PM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM

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

All times are GMT -5. The time now is 01:00 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