Help with awk statement

Hi

I have a file with

test test2 1000000657373
test1 test3 1000003849450
test2 test4 test5 100000837474

I cat the file and pipe it to an awk statement like so

cat /tmp/file |awk '{if ($3 ~ "^[0-9][0-9]*$" && $3 > 1024000000) print "/vol/"$1"/"$2;else if ($4 ~ "^[0-9][0-9]*$" && $4 > 1024000000) print "/vol/"$1"/"$2 " "$3}'

Which the output is

/vol/test/test2
/vol/test1/test3
/vol/test2/test4 test5

How can I get it to print something at the begining and in between to look like

INSERT
/vol/test/test2
INSERT
/vol/test1/test3
INSERT
/vol/test2/test4 test5

Thanks and much appreciated

echo 1 | awk '{print "INSERT\n" $1}'

I want to do this all in the awk command

awk '
        {
                printf "INSERT" RS OFS "vol" OFS
                for ( i = 1; i <= (NF - 1); i++ )
                        s = s ? s OFS $i : $i
                printf "%s\n", s
                s = ""
        }
' OFS="/" file

Thanks I figured it out

</tmp/file awk '{if ($3 ~ "^[0-9][0-9]*$" && $3 > 1024000000) print "INSERT\n/vol/"$1"/"$2;else if ($4 ~ "^[0-9][0-9]*$" && $4 > 1024000000) print "INSERT\n/vol/"$1"/"$2 " "$3}'

that was provided as a hint...

1 Like

I have another question. In my original awk statement I took for granted that there won't be more that 4 fields. However I ran into a situation where I won't know how many fields there can be.
Example

test2 test4 test5 100000837474

could be

test2 test4 test5 test6 test7 100000837474

So I was thinking how can I check the line for the first occurrence of a numeric variable.?

---------- Post updated at 12:14 PM ---------- Previous update was at 12:07 PM ----------

I have another issue where I thought that I won't have more than 4 fields. It turns out I won't know how many fields I will have. How can I check the line for the first occurrence of a numeric variable with awk??

Did you try post#4 ?

And why do you want to check the first occurrence for numeric?

--ahamed

I did not try #4. I figured that out.
What I need to do now is look at the number in my file and determine if it is bigger than 1TB. However it now appears that it won't only be 4 fields. It can be more than that.
So I need a way to check the number.
I figured if I can fo through the line and when it is numeric check the size.

Orginal file

test test2 1000000657373
test1 test3 1000003849450
test2 test4 test5 100000837474

However my file can also look like

test test2 1000000657373
test1 test3 1000003849450
test2 test4 test5 test6 test7 100000837474

and maybe more columns.

Check $NF.

Use $NF to access the last field.

--ahamed

Having hard time making the NF work. One of my line of output is

tree         8 vf_MTLHQNASF09_Wkgp3 Agile Delivery Services   2980176 1572864000      79       - /vol/vf_MTLHQNASF09_Wkgp3/Agile Delivery Services

So how do I get

/vol/vf_MTLHQNASF09_Wkgp3/Agile Delivery Services

this. As you can see there are spaces. And this is not constant. I would not know what that would be.

How does this (from post #10) fit to your post #13? (<--- GIANT question mark)

I tried to rearrange the output but now I put the actual output on. So to make it easier to understand. So the actual output is what I put. Been trying all sorts of stuff.
I don't use awk that often so I don't know all the tricks to it.

So please rephrase your initial problem carefully, based on real data, and supply a representative input file and a desired output. There is nothing more annoying than to work on "moving targets", i.e. a sample file that all of a sudden is not valid any more and you have to start over with proposals!

To get at number fields using awk, try the following (you can e.g. break after the first number encountered):

awk '{for (i=1; i<=NF; i++) if ($i+0==$i) print $i}' file
8
2980176
1572864000
79