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

may be this

{
   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

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

Oops duplicate

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 :slight_smile:

 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.

Hi devmiral,

File name is not of fixed length, it'll keep changing.

may be this for variable length file name

{
   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

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

Hi reborg,

I am sorry as I was not clear enough.
"File size" means: calculate the size of the file in terms of bytes excluding first 2 lines, store that in a variable say $four and comparing it with $two (as I mentioned earlier) and generate error code say 150 if it doesn't match.

What in that case what radoulov posted should do what you want.