LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 09: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, 09: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: 332Reputation: 332Reputation: 332Reputation: 332
Use 'head':
Code:
variable=$(someCommand | head -n1)
 
1 members found this post helpful.
Old 11-02-2011, 09: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, 06:22 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 06:27 AM.
 
1 members found this post helpful.
Old 11-03-2011, 07: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


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


All times are GMT -5. The time now is 04:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration