Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read the contents of a file into a awk variable

Status
Not open for further replies.

vlad9999

Programmer
Apr 19, 2010
23
FR
How to store into a awk variable the contents of a small file, which has several lines (obviously without affecting its content - the line breaks, the successive spaces, etc) ?

In fact I must make with awk the equivalent of
Code:
cat file | sed "s|TOTO|$toto|g;s|TITI|$titi|g;s|TATA|$tata|g"
the variables toto, titi, tata being calculated in awk.

I intend to do in awk
Code:
#[b]awk code[/b]:
sub("TOTO",toto,s);sub("TITI",titi,s);sub("TATA",tata,s);print s
but for that I must be in the variable s the file contents, the equivalent of
Code:
s=$(cat file)

Some one can help me?
 
In the awk's man page have a look to the getline function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That means I have to read the file line by line and concatenate the results ?
 
Hi

Or you can use a dirty trick :
Code:
awk -vRS=theInputFileNotContainsThisString '{print "the whole input :",$0}' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

But I suggest to process the file line by line.

Feherke.
 
vlad9999 said:
That means I have to read the file line by line and concatenate the results?

That is exactly how awk (and sed for that matter) works. But you don't have to worry about the concatenation bit, awk does that for you.

[tt]awk '{sub(this...); sub(that...); sub(theother...); print}' /path/to/ifile >/path/to/ofile[/tt]

HTH,

p5wizard
 
Yes, by playing on the RS I simplify the program.
Code:
BEGIN {RS="@" ; getline s1 < m2_file ; close(m2_file) ; RS="\n"}

(the variable m2_file is transmitted with option -v

Thank you !!
 
Finally, my program:
Code:
  cat $in_file | awk -v m2_file=$m2_file '
    BEGIN {RS="@" ; getline s1 < m2_file ; close(m2_file) ; RS="\n"}
    {
    toto=substr($0,12,27) ; gsub(/ *$/,"",toto)
    titi=substr($0,81,11)
    tata=substr($0,242,3)
    s2=s1
    gsub("TOTO",toto,s2)
    gsub("TITI",titi,s2)
    gsub("TATA",tata,s2)
    printf s2
    }
  ' >> $out_file
It retrieves information from a flat file, removes trailing spaces for some and then puts these values in another form, described by the "model" file m2_file.

Of course we must be sure that the character "@" does not exist in the file m2_file, it is the case.

thank you again feherke
 
You could remove the UUOC:

Code:
  awk -v m2_file=$m2_file '
    BEGIN {RS="@" ; getline s1 < m2_file ; close(m2_file) ; RS="\n"}
    {
    toto=substr($0,12,27) ; gsub(/ *$/,"",toto)
    titi=substr($0,81,11)
    tata=substr($0,242,3)
    s2=s1
    gsub("TOTO",toto,s2)
    gsub("TITI",titi,s2)
    gsub("TATA",tata,s2)
    printf s2
    }
  ' [red]$in_file[/red] >> $out_file

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top