How to read a line and put it into 3 variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a line and put it into 3 variables
# 1  
Old 04-20-2007
How to read a line and put it into 3 variables

Hi All,

I'll get a file whose 2nd line contains 3 fields: filename(variable length), file size char(10), and record count int(10). How do I cut it and put it into 3 variables?

eg: abcd.csv01234567891111111111

now I want: $one = abcd.csv, $two = 0123456789, $three = 1111111111.

I also want to check for the file size (excluding first 2 lines of the file) and put it in another variable say $four. If the variable $two doesn't match with $four then generate an error code say: 150.
Any Help would be appreciated.


Regards,
Mandab
# 2  
Old 04-20-2007
may be this

Code:
{
   f[1]=substr($0,1,8)
   f[2]=substr($0,9,10)
   f[3]=substr($0,18,length)
   printf("The seperate variable are  :\n%-10s\n%-10s\n%-10s\n",f[1],f[2],f[3])
}

the output
Code:
njundmdse19 371 $nawk -f file_script.sc file1
The seperate variable are  :
abcd.csv
0123456789
91111111111

devmiral
# 3  
Old 04-20-2007
Oops duplicate
devmiral
# 4  
Old 04-20-2007
Code:
awk 'NR == 2 {
        one = substr($0,1,length-20)
        two = substr($0,length-19,10)
        three = substr($0,length-9)
        tempfile = FILENAME ".tmp"
        scmd = ("du " tempfile)
        rmcmd = ("rm " tempfile)
        }
NR>2 {
        print  > tempfile
        close(tempfile)
        }
END {
        scmd | getline 
        close(scmd)
                if ( $1 != two )
                        exitval = 150 
                system(rmcmd)
                exit exitval
           
}' inputfile

Use nawk on Solaris.

P.S. Assuming, of course, FILENAME.tmp doesn't exist Smilie

Last edited by radoulov; 04-23-2007 at 08:37 AM..
# 5  
Old 04-20-2007
Code:
 eval `sed '2s_\(.*\)\(.\{10\}\)\(.\{10\}\)$_one=\1 two=\2 three=\3_' file`

You haven't given enough information about what you mean by "file size" to be able to answer the second part.
# 6  
Old 04-20-2007
Hi devmiral,

File name is not of fixed length, it'll keep changing.
# 7  
Old 04-20-2007
may be this for variable length file name
Code:
{
   f[1]=substr($0,1,match($0,/[0-9]+/)-1)
   f[2]=substr($0,match($0,/[0-9]+/),10)
   f[3]=substr($0,length(f[2])+length(f[1])+1,length)
   printf("\nThe seperate variable are :\n%-10s\n%-10s\n%-10s\n",f[1],f[2],f[3])
}

the out put is

Code:
njundmdse19 411 $nawk -f file_script.sc file1

The seperate variable are  :
abcd.csv
0123456789
1111111111

The seperate variable are  :
ab.csv
0123456789
1111111111

Hope that helps
devmiral
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read line and save fields as variables

Hej guys, I am trying to read a csv file line by line, save it's fields as variables per line so I can use them as parameters and execute stuff. I am new to shell scripting and was just strictly following a tutorial. Somehow my version seems to ignore the loop. Any help? TY! :) #!/bin/bash... (12 Replies)
Discussion started by: Splinter479
12 Replies

2. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

3. Shell Programming and Scripting

read line by line and calculate the co-presence of variables

Hey guyz, I have a table which shows the presence or absence of my variables (A,B,C,...) in my observations (1,2,3,...) * A B C ... 1 1 0 1 2 1 1 0 3 1 0 0 ... I want to calculate the co-presence of my variables. to have a table shows the pairwise presence of the variables (have... (1 Reply)
Discussion started by: @man
1 Replies

4. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

5. Shell Programming and Scripting

Read 1-line file and separate into multiple variables

I have one line files with 17 records separated by a semi-colon. I need to create a variable from each record, which I can do via a separate awk for each one, but I know there has to be a better way. Along with pulling out the variable, I need to convert some url coding like a + to a space, etc.... (4 Replies)
Discussion started by: numele
4 Replies

6. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

7. Shell Programming and Scripting

read a file in shell and put result in a line

Hi All, I have a to read a file and put the result in one line. The i am reading from contain the data like below. 1 signifies the beging of the new line. (6 Replies)
Discussion started by: lottiem
6 Replies

8. Shell Programming and Scripting

Read variables from line to fixed length

I would like to make a script to read three variables (no fixed length or position) from a line and write them into a file, with fixed length and right-justified in each column. The fixed text (text1-text4) prior to the thee variables and the variables themselves are originally separated by spaces... (3 Replies)
Discussion started by: SharkM
3 Replies

9. Shell Programming and Scripting

Put content of file into variables

How to put a line of strings (with some white spaces in between) from a file into variables? I have tried the following codes. However, the content is cut by space (not by line) for i in `cat ${source_file}` do echo $i done Please comment. Thanks. (2 Replies)
Discussion started by: Rock
2 Replies

10. UNIX for Dummies Questions & Answers

How can I put wildcards in an if statement that uses variables?

With the if statement: if How can I make it so it accepts a wildcard after the ${CURR_DAY_MONTH} variable? Putting a -f /webtrends/SUN/mrw2/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* won't work, right? I think I need some kind of special character so it knows the wildcard is... (3 Replies)
Discussion started by: LordJezo
3 Replies
Login or Register to Ask a Question