Copy part of a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy part of a variable
# 1  
Old 11-09-2009
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  
Old 11-09-2009
this code works in bash...

Code:
f=$(grep  "<TIME>"  input.txt | tail -1)

Code:
echo $f
<TIME>16:15:48</TIME>

# 3  
Old 11-09-2009
Hey thanks this code works equally good.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy part of file between two strings to another

I am a newbie to shell scripting I have a large log file , i need to work on the part of the log file for a particular date. Is there a way to find the first occurance of the date string and last occurance of the next day date date string and move this section to a new file. to explain it... (3 Replies)
Discussion started by: swayam123
3 Replies

2. UNIX for Dummies Questions & Answers

Copy the last part since the file has been updated

Input File1 constatntly running and growing in size. My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 123 terminated **ID PIN 12345 Comamnd Successful Command Terminated Command Successful Command Terminated **My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 345... (3 Replies)
Discussion started by: eurouno
3 Replies

3. Shell Programming and Scripting

Using the part of a line as a variable?

Hello Friends, I need a command (or script line) that allows me to use of a part of line (given by me) as a variable. Let us assume the name of the command is MYCMD. When I type MYCMD fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/ it must do the following job cd /homefolder/hhk/ADS/ ... (18 Replies)
Discussion started by: rpf
18 Replies

4. UNIX for Dummies Questions & Answers

Copy a part of file

Hi, I want to copy text between expressions ">bcr1" and ">bcr2" to another file. Any simple solutions? Thanks (4 Replies)
Discussion started by: alpesh
4 Replies

5. UNIX for Dummies Questions & Answers

Removing Part of a variable based on a pattern

Hi All, I am writing a script to work with files in a folder. The files are all in the following patterns (without quotes): "some filename - NxNN - the end.YYY" or "some filename - NNxNN - the end.YYY" Where N = a single number and YYY is the extension. Basically what I want... (5 Replies)
Discussion started by: sgtbobie
5 Replies

6. Shell Programming and Scripting

How read the part of the string into a variable?

Hi, I'm using bash and brand new to shell script. I would like to do the following. I have a string which is "UPDATE=1.0". I would like to read the value "1.0" alone in a variable. i.e the things afer "=" How do I do that? Thanks, (1 Reply)
Discussion started by: scriptfriend
1 Replies

7. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

8. Shell Programming and Scripting

Perl or Awk script to copy a part of text file.

Hi Gurus, I'm a total newbie to Perl and Awk scripting. Let me explain the scenario, there is a DB2 table with 5 columns and one of the column is a CLOB datatype containing XML. We need all the 4 columns but only a portion of string from the XML column. We decided to export DB2 table to a .del... (26 Replies)
Discussion started by: asandy1234
26 Replies

9. Shell Programming and Scripting

Repacing part of string with a variable

I have following strings in a file DUPTASMTRMMBAL,20070416200704160117232101172321,,,,,,,@@@Y DUPTASMTRMMCON,20070416200704160127189901271899,,,,,,,@@@Y DUPTASMTRMMHG,,20070416200704160112051001120510,,,,,,,@@@Y What i need to do is replace the date 20070416 with anoth date which is stored in... (4 Replies)
Discussion started by: divz
4 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question