Computing.Net > Forums > Unix > Unix script & AWK

Unix script & AWK

Reply to Message Icon

Original Message
Name: abd73fr
Date: December 9, 2004 at 07:36:13 Pacific
Subject: Unix script & AWK
OS: Mac OS X.3
CPU/Ram: PowerPc G4/512 MB
Comment:

Hi all,

I am writting a shell script, but I have a problem. :(

#!/bin/sh

CelDist=5

CelDistC=`awk '{if (NF>0) print ($0)}' /Users/Me/
Desktop/temps/ee2`

awk -v CelDist="$CelDist" -v CelDistC="$CelDistC" '{if
(CelDist != "2") gsub ("blabla", CelDistC); print ($0)}' /
Users/Me/Desktop/temps/ee3


I want to put the file (ee2) into the variable (CelDistC),
then replace a word (blabla) in the file (ee3) by the
contents of the file (ee2)..

if the file (ee2) has only one line, it works. But If it has
many lines I get this error:

awk: newline in string The fisrt ... at source line 1


How can I put a file into a variable in a shell script?

Thanks


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: December 9, 2004 at 08:33:41 Pacific
Subject: Unix script & AWK
Reply: (edit)

When ee2 contains multiple non-blank lines, those lines are assigned to the CelDistC variable, thus that variable will in fact contain one or more newline characters, which obviously is causing problems with your awk.

What exactly do you want to capture from the ee2 file?  If you want only the first non-blank line from ee2, then exit after that first print command:

CelDistC=`awk '{if (NF>0) {print ($0); exit} }' ee2`

If you want to capture all the lines in ee2, after you capture CelDistC, you can eliminate the newlines with:

CelDistC=`echo $CelDistC`


Report Offensive Follow Up For Removal

Response Number 2
Name: abd73fr
Date: December 9, 2004 at 09:29:37 Pacific
Subject: Unix script & AWK
Reply: (edit)

Thanks Jim,

In fact, I want to captur ALL the lines in ee2, and put them
AS THEY ARE, in the place of the word (blabla) in the file
ee3.

If I put:
CelDistC=`echo $CelDistC`

All the lines in ee2 will be printed in ee3 as ONE line only
... :(

I wish there is another way to do that...

Thanks again


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim Boothe
Date: December 9, 2004 at 11:40:26 Pacific
Subject: Unix script & AWK
Reply: (edit)

Well, actually, the code you posted works on HP-UX exactly like you want - it will substitute multiple lines for blabla.

But your awk does not like the newlines in the variable (maybe the gsub command?).

If you have nawk (new awk), try that. In the meantime, me or someone will post an alternate solution.


Report Offensive Follow Up For Removal

Response Number 4
Name: Jim Boothe
Date: December 9, 2004 at 14:49:02 Pacific
Subject: Unix script & AWK
Reply: (edit)

Here is an alternate solution which also works for me.  I don't know if it will work for you because I am not encountering your problem.  When constructing the CelDistC variable, rather than have awk print multiple lines (each terminated with a newline), I have awk separate the lines with the symbolic expression for newline (\n).

Give it a try.

CelDist=5

CelDistC=`awk '{
 if (NF>0)
    if (notfirst==1)
       printf "\\n" $0
    else
      {printf $0
       notfirst=1}
}' ee2`

awk -v CelDist="$CelDist" -v CelDistC="$CelDistC" '{
if (CelDist != "2")
   gsub ("blabla", CelDistC)
print ($0)
}' ee3


Report Offensive Follow Up For Removal

Response Number 5
Name: abd73fr
Date: December 10, 2004 at 01:40:44 Pacific
Subject: Unix script & AWK
Reply: (edit)

Dear Jim, thanks a lot...

Because I am writing a shell script, I had to write your
code in two lines. So I wrote it as follows:

CelDistC=`awk '{if (NF>0); if (notfirst==1) printf ("\\n"
$0) ; else {printf $0; notfirst=1}}' ee2`

awk -v CelDist="$CelDist" -v CelDistC="$CelDistC" '{if
(CelDist != "2") gsub ("blabla", CelDistC); print ($0)}' ee3

but I get the same error as before..

As a work around, I wrote another code which is
ridiculous, but it works :)

CelDistC=`awk '{if (NF>0) Array[j]=$0; printf
(Array[j]"@@@")}' ee2`

awk -v CelDist="$CelDist" -v CelDistC="$CelDistC" '{if
(CelDist != "2") gsub ("blabla", CelDistC); gsub ("@@@",
"\n");print ($0)}' ee3

Thanks again for your help :)


Report Offensive Follow Up For Removal


Response Number 6
Name: Jim Boothe
Date: December 10, 2004 at 07:08:07 Pacific
Subject: Unix script & AWK
Reply: (edit)

Good job - nice thinking.

Actually, when constructing your variable, storing and printing from the array does nothing for you.  Using printf instead of print is the key, as it keeps the newlines out of it at this point.  You can achive the same results with the simplified:

CelDistC=`awk '{if (NF>0) printf $0 "@@@"}' ee2`

But since you are appending @@@ to each and every line from ee2, your end result will have line breaks after each ee2 line, even if only one ee2 line.  If you want only line breaks BETWEEN ee2 lines and not after the only or final ee2 line, then do this instead:

CelDistC=`awk '{
 if (NF>0)
    if (notfirst==1)
       printf "@@@" $0
    else
      {printf $0
       notfirst=1}
}' ee2`


Report Offensive Follow Up For Removal

Response Number 7
Name: abd73fr
Date: December 10, 2004 at 08:33:42 Pacific
Subject: Unix script & AWK
Reply: (edit)

Thanks again Jim,

Your simplified code works fine and is better than mine :)

I am not programmer, but I try to use what I know to do
what I want...

Thanks to you I learned somethings useful :)

@+


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Unix script & AWK

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software