0

i couldn't solve this. when i execute this program i get the following error " line 7: unexpected EOF while looking for matching `'' "

a=115292a1504606846976ULL
b=2
if [ "$b" = "2" ]; then
var1=`echo $a | awk -F"U" '\
{
var2=`echo $var1 | awk -F"a"
{print " "$2}'`
}\
fi

Update: from other, recently closed question

To be more specific, this is my project code

if [ "$FORMAT" = "java" ]; then
        cat $INPUT_FILE | awk -F":" '\
                /^$/ { print "" }\
                /^\/\/.*/ { print "     "$0}\
                /:string:/ { print "    public static final String "$1" = "$3";" }\
                /:char:/   { print "    public static final char "$1" = "$3";" }\

/:ullong:/ { print "    public static final long "$1" = "$3";" }\
                /:ulong:/  { print "    public static final int "$1" = "$3";" }\
                /:long:/   { print "    public static final int "$1" = "$3";" }\
        ' >> $CONST_FILE
fi;

Now i need to truncate $3 (this value is actually read from another file) into two parts(only for ullong). lets say

$3=1256985361455ULL

i need to truncate into 1256985361455 and ULL. (only when it is ullong) please help me out in this issue.

i tried using another awk inside the the following, but ended up in chaos.

/:ullong:/ { print "    public static final long "$1" = "$3";" }\
5
  • What are you trying to do? The above snippet makes little sense. Commented Apr 19, 2011 at 12:34
  • i need to truncate the occurence of 'U' first and then from the output of that first awk command i need o truncate the occurence of 'a'. i'm just trying to provide this as an example because i need to use the similar method method in my project Commented Apr 19, 2011 at 12:41
  • So what is your final output after "truncating" ? Do you mean removing all the "a"s and "U"s ? Please ask your question clearly, providing examples of output you want where necessary. Commented Apr 19, 2011 at 12:45
  • Use multiple pipes - see sample in my answer Commented Apr 19, 2011 at 12:45
  • In the body of your awk script, there's no need to put a continuation character (\) at the end of every line. Commented Apr 19, 2011 at 15:20

5 Answers 5

2

If you expect the value of $3 for the ullong records to be something like "1256985361455ULL" then

/:ullong:/ { 
    sub(/ULL$/, "", $3)
    print "    public static final long "$1" = "$3";" 
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Logeshwari, now it's time to accept some answers for your questions.
2

Your quoting problem is because once you start a back-quoted command, it continues until the next back-quote. This is your code as shown above, except I've removed the blank lines.

a=115292a1504606846976ULL
b=2
if [ "$b" = "2" ]; then
var1=`echo $a | awk -F"U" '\
{
var2=`echo $var1 | awk -F"a"
{print " "$2}'`
}\
fi

(Back-quotes '`' are hard to show in in-line Markdown.)

The line var1= line starts a back-quoted expression, which stops at the next unescaped back-quote, which is the one after var2=. It then reads the rest of that line, and on the following line, encounters a single quote. When it looks for the following single quote, there is none - so it reports an error. You can demonstrate this is what goes on in steps:

a=115292a1504606846976ULL
b=2
if [ "$b" = "2" ]; then
var1=`echo $a | awk -F"U" '\
{
var2=\`echo $var1 | awk -F"a"
{print " "$2}'`
}\
fi

The script above has an escape (backslash) before the back-quote after var2=, so now the command in back-quotes extends to the back-quote after the print line. This still isn't valid shell; the line with }\ combines with the fi to make a command name }fi, so you still get an unexpected EOF error - because the fi for the end of the if is missing. Modify the script again:

a=115292a1504606846976ULL
b=2
if [ "$b" = "2" ]; then
var1=`echo $a | awk -F"U" '\
{
var2=\`echo $var1 | awk -F"a"
{print " "$2}'`
#}\
fi

This comments out the close brace, and the shell script is now 'valid'; it is awk's turn to start complaining about the invalid script it is given.

++ awk -FU '{
var2=`echo $var1 | awk -F"a"
{print " "$2}'
awk: syntax error at source line 2
 context is
     >>> var2=` <<< 
awk: illegal statement at source line 2
awk: illegal statement at source line 2
    missing }

Other people have given you roughly what you need as an answer. I'd probably use Perl to do the splitting up (and I suspect I could lose the intermediate array @x if I spent enough time on it, producing a script of line noise):

a=115292a1504606846976ULL
b=2
if [ "$b" = "2" ]
then var1=$(echo $a | perl -ne '@x=split /[aU]/; print "$x[1]\n"')
fi

However, you can also do it in one line with awk, thus:

a=115292a1504606846976ULL
b=2
if [ "$b" = "2" ]
then var1=$(echo $a | awk -Fa '{sub("ULL","",$2); print $2}')
fi

This splits the input on the 'a' instead of the 'U'; it then removes the 'ULL' from the second field and prints it. If you want to split on 'U', then you use:

a=115292a1504606846976ULL
b=2
if [ "$b" = "2" ]
then var1=$(echo $a | awk -FU '{sub("[0-9]+a", "", $1); print $1}')
fi

The regular expression in the sub is marginally more complex this way.

Comments

1

Not sure exactly what you are trying to do, but this slight re-write printed out the middle part of a (which is I think what you wanted)

> cat moo.sh
a=115292a1504606846976ULL
b=2
if [ "$b" = "2" ]; then
        var1=`echo $a | awk -F"U" '{print $1}'`
        var2=`echo $var1 | awk -F"a" '{print " "$2}'`
        echo $var2
fi
> sh moo.sh
1504606846976

Comments

0

You can't do shell commands inside awk except to make system calls. without you telling us what you are trying to achieve

#!/bin/bash
a=115292a1504606846976ULL
b=2
case "$b" in
 2 ) 
    a=${a%U*}
    echo ${a#*a} # I assume you want to get 1504606846976
esac

Comments

0
a=115292a1504606846976ULL
b=2

if [ "$b" = "2" ]; then
  var1=`echo $a | awk -F "U" '{print $1}' | awk -F "a" '{print $2}'`
fi

2 Comments

If awk needs to be used, there's no need to invoke 2 instances of it. you can either do a sub/gsub/split on $1 in the first awk , or use -F"[Ua]"
@kurumi: No, but this illustrates how to do what he needs in a readable way, using only what the OP demonstrates an understanding of. sed would either way be the best choice for this IMO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.