The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Perl or Awk script to copy a part of text file. asandy1234 Shell Programming and Scripting 26 2 Weeks Ago 12:17 PM
Set specific part in command output into variable orit Shell Programming and Scripting 7 08-16-2008 10:34 AM
direction symbol in a variable as part of the command peterloo Shell Programming and Scripting 5 12-06-2007 07:25 PM
Repacing part of string with a variable divz Shell Programming and Scripting 4 07-19-2007 01:09 AM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 11:29 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
Shellslave Shellslave is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 10
Copy part of a variable

Hi,

i was using a input file to get the last line of the file.But now i have stored
the values from the file to a variable and want the last line from the variable . Slightly confused on how to extract that data from the variable.

previous code,
Code:
 
cat input.txt
<TIME>00:15:48</TIME>
asderffgg
<TIME>15:15:20</TIME>
sdfrtghy
<TIME>22:14:40</TIME>
sderrthgh
<TIME>18:19:32</TIME>
sfrtryhbhgh
<TIME>16:15:48</TIME>
 
grep "<TIME>" input.txt > temp.txt
 
cat temp.txt
<TIME>00:15:48</TIME>
<TIME>15:15:20</TIME>
<TIME>22:14:40</TIME>
<TIME>18:19:32</TIME>
<TIME>16:15:48</TIME>
echo `tail -1 temp.txt` >temp.txt
 
o/p:
in the temp file i got <TIME>16:15:48</TIME>
now i dont want to use this temp file instead i want to put these in a variable and get the same output in that variable.
any help will be appreciated

---------- Post updated at 02:37 PM ---------- Previous update was at 02:29 PM ----------

Now i am trying to remove the temp file and use variable to do all the work done by temp file.

code now,
Code:
h=`grep "<TIME>" input.txt`
f=`echo $h|tail -1 `
this code is not working
i get
echo $h
<TIME>00:15:48</TIME> <TIME>15:15:20</TIME> <TIME>22:14:40</TIME><TIME>18:19:32</TIME> <TIME>16:15:48</TIME>

and also $f
<TIME>00:15:48</TIME> <TIME>15:15:20</TIME> <TIME>22:14:40</TIME><TIME>18:19:32</TIME> <TIME>16:15:48</TIME>
  #2 (permalink)  
Old 1 Week Ago
protocomm protocomm is offline
Registered User
  
 

Join Date: Mar 2009
Location: france, montpellier
Posts: 150
this code works in bash...

Code:
f=$(grep  "<TIME>"  input.txt | tail -1)
Code:
echo $f
<TIME>16:15:48</TIME>
Bits Awarded / Charged to protocomm for this Post
Date User Comment Amount
1 Week Ago Anonymous Thanks protocomm 100
  #3 (permalink)  
Old 1 Week Ago
Shellslave Shellslave is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 10
Hey thanks this code works equally good.
Sponsored Links
Reply

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 08:06 PM.


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