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
# 8  
Old 06-16-2006
A workaround

Code:
#! /bin/sh

val=\
"a      b       c       trk_012345678"
echo "$val"
num=${val##*_}
rest=${val%$num}
final=`expr $num + 1 `
if [[ ${#num} > ${#final} ]] ; then
echo "$rest"0"$final"
else
echo "$rest$final"
fi ;

# 9  
Old 06-16-2006
Oops Vino, sorry we forgot the main purpose was to pick this from a file ie 'test'...

in our examples we used that from a variable ie val..

let me reinstate..

'test' contains:

a<tab>b<tab>c<tab>Trk_01234567

and now we have to do pick and manipulate the '01234567' part..and store the result in 'test1'

So now kindly let me know the changes i have to make to your script..thanks in advance

Last edited by shiroh_1982; 06-16-2006 at 08:08 AM..
shiroh_1982
# 10  
Old 06-16-2006
What does the file test look like ? I know it has that pattern. What else ?


Code:
#! /bin/sh

while read val
do
echo "$val"
num=${val##*_}
rest=${val%$num}
final=`expr $num + 1 `
if [[ ${#num} > ${#final} ]] ; then
echo "$rest"0"$final" > test1
else
echo "$rest$final" test1
fi ;
done < test

# 11  
Old 06-16-2006
thanks Vino..Nothing else..I got all i needed.
shiroh_1982
# 12  
Old 06-16-2006
Awk is good at this kind of thing...
Code:
$ cat test
a<tab>b<tab>c<tab>Trk_01234567

$ awk -F_ '{printf "%s_%0*d\n", $1, length($2), ++$2}' test > test1

$ cat test1
a<tab>b<tab>c<tab>Trk_01234568

 
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