Capturing some data from a file into a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Capturing some data from a file into a variable
# 1  
Old 06-15-2006
Capturing some data from a file into a variable

I have a file with some values in a tab delimted format

Eg:
'test' contains:

a<tab>b<tab>c<tab>Trk_12345678

now i need to capture this value 'Trk_12345678' into a variable say 'x' and append that value of 12345678 to 12345679 and store is back to a new 'test1' file as :

'test1' contains:

a<tab>b<tab>c<tab>Trk_12345679

Can someone kindly help me on this.I kinda know u have to use commands like sed,cut etc..i jst need some insight of the exact usage.

Thanks.
shiroh_1982
# 2  
Old 06-15-2006
I'm not sure what you mean by 'append'. Are you always replacing the last digit by 9, which is not an append? Or are you adding 1 to the number, which is not an append either? or what?

Are you using the bash or sh shell? These scripts use bash built-in commands and string handlers, which are much faster than launching sed all the time. The IFS=$'\x09' tells it the data is tab-delimited, and the 'read' command reads each field into each variable listed after it. ${VAR4:offset:length} returns a substring of VAR4, ${VAR4:offset} returns the whole string after the given offset, and $((EXPR)) evaluates a mathematical expression.

This version just replaces the last digit by 9.
Code:
# IFS=$'\x09' read VAR1 VAR2 VAR3 VAR4 < test
# echo -e "${VAR1}\t${VAR2}\t${VAR3}\t${VAR4:0:11}9" > test1

This version adds 1 to the value.
Code:
# IFS=$'\x09' read VAR1 VAR2 VAR3 VAR4 < test
# X=${VAR4:4}
# echo -e "${VAR1}\t${VAR2}\t${VAR3}\tTrk_$((X+1))" > test1

# 3  
Old 06-16-2006
Thanks for the reply Corona.To conrirm, i wanted to append the numeric value by 1...

Quote:
Originally Posted by Corona688
This version adds 1 to the value.
Code:
# IFS=$'\x09' read VAR1 VAR2 VAR3 VAR4 < test
# X=${VAR4:4}
# echo -e "${VAR1}\t${VAR2}\t${VAR3}\tTrk_$((X+1))" > test1


I tried the same exact code you gave me but I was getting the following output:
a<tab>b<tab>c<tab>Trk_432511

Please note that in input was Trk_01514576.Another point to note is that id the input is Trk_01514576 ,i would like the output to be Trk_01514577.(please note the 0 in the header of the number has to be maintained)..

Thanks in advance.

Last edited by shiroh_1982; 06-16-2006 at 06:41 AM..
shiroh_1982
# 4  
Old 06-16-2006
Code:
[/tmp]$ cat try.ksh
#! /bin/ksh

val=\
"a      b       c       trk_012345678"
echo "$val"
num=${val##*_}
rest=${val%$num}
typeset -Z${#num} final
((num = num+1));
final=$num
echo "$rest$final"
[/tmp]$ ./try.ksh
a       b       c       trk_012345678
a       b       c       trk_012345679

If you want redirect the output to another file, then use
Code:
echo "$rest$final" > test1

# 5  
Old 06-16-2006
Thanks for the reply Vino...Im using the bash shell..

copied the code into vino.sh and when i ran that script got the following output:

./vino.sh

a b c trk_012345678
./vino.sh: typeset: unknown option: `-Z'
typeset: usage: typeset [-afFrxi] [-p] name[=value] ...
./vino.sh: ((: 012345678: value too great for base (error token is "012345678")
a b c trk_012345678
shiroh_1982
# 6  
Old 06-16-2006
Did you change the shebang #! /bin/ksh to #! /bin/bash

Dont you have ksh on your machine ? If you do, then do not change the shebang.
# 7  
Old 06-16-2006
I tried another way:

val=\
"a b c trk_012345678"
echo "$val"
num=${val##*_}
echo $num
rest=${val%$num}
echo $rest
final=`expr $num + 1`
echo "$rest$final"

However i still want to maintain that '0' in the header which gets ommited in the end..
shiroh_1982
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : not capturing all the data from excel sheet

Hi folks, I am working on assignment that captures all the records(2 columns one column contains names and other contain date of birth) from excel sheet stored in a directory and checks for current date and month. If it matches current date and month then the matched records are printed as... (1 Reply)
Discussion started by: giridhar276
1 Replies

2. Shell Programming and Scripting

capturing selective data from a vcd file

Hi, This is a vcd file.A vcd file may have 'n' modules. 1) I need to capture the data in bold,i.e. the module names (shown in bold) 2) Also i need to capture the data inside each individual module,say for tst_bench_top ,i need to capture data from line 4 to line 20 ... I just want one... (2 Replies)
Discussion started by: veerabahu
2 Replies

3. Red Hat

capturing data of nethogs

I have installed nethogs to see which process is sending load on the network. i would like to know how can i capture its data in a log file. Or is there is a unix command like script or tee which can help me to capture the data using cronjob (0 Replies)
Discussion started by: anshus1
0 Replies

4. Shell Programming and Scripting

Capturing data between patterns

Hii, Friends, I want your help in one of my problem. My problem is as follows. I have a flat file as follows (Just a sample) MHT011 01(DOT)8750707asdfas8609 03(DOT)ASD3453ASD 09(DOT)876JHT87 11(DOT)sfd324ert TTT077 01(MOB)876786klj897 06(MOB)876JHT87 07(MOB)sfd324ert... (4 Replies)
Discussion started by: anushree.a
4 Replies

5. Shell Programming and Scripting

capturing a file to store in a variable

I have a file in a windows directory the file is delivery to us like this 07210900.dat where 07210900 is the current date. If I want to store that file in a variable UpLoadFileName and rename, so I can Ftp later to a UNIX directory, I am doing this, is this correct? CDRemoteDir='cd... (0 Replies)
Discussion started by: rechever
0 Replies

6. Shell Programming and Scripting

Capturing Particular Data

Hi everyone! Can any one help me out regarding capturing relevant data from a file. For e.g, if i want to capture the comment written after "Prompt:" for a particular date, like for this case what would be the command to capture the tag written after "Prompt:" for Date: 2009-03-20. A... (7 Replies)
Discussion started by: muhmsida
7 Replies

7. UNIX for Dummies Questions & Answers

Capturing a value in to variable.

Hi, I currently have a shell script where it captures in the value in to 'TOTAL' and outputs to a file, i want to capture another value of a query in to another variable and output it. Current script: sqlplus -s $user <<EOD | read TOTAL set heading off set pages 0 set feedback off set... (1 Reply)
Discussion started by: ravi0435
1 Replies

8. Shell Programming and Scripting

capturing the o/p to a variable

Hi, #Script mentioned below txt=($(echo `./demo1.sh`)) p=0 for p in "$txt" do col=($(./generateTable /import/data01/sri/Developer/SqlReport/Lab/23/${var} "$p")) . . . . done o/p displayed upon executing a script called "demo1.sh" is as below(two seperate lines):... (2 Replies)
Discussion started by: villain41
2 Replies

9. Shell Programming and Scripting

Capturing Unix Traceroute data

Hi guys, I need a way to capture the host on the next-to-last hop in a traceroute output. The last output is the destination but I need to capture the router just before the last hop. I can do this in perl but I'm not so sure about Shell... I'm on AIX 5.3 using ksh any ideas? ... (3 Replies)
Discussion started by: TivoliGUY
3 Replies

10. Shell Programming and Scripting

Capturing value into variable

Hi, I am new to shell scripting, I am trying to write a shell script, which will automate the process of mailing the invoices at the end of the day. For major part I am using Oracle database function. The problem is I want to capture the value returned by the Oracle function into the script... (2 Replies)
Discussion started by: prasad01
2 Replies
Login or Register to Ask a Question