Hello Community,
i have a litte Problem with concating a string in an awk command.
val_arr=( $( cat myfile | awk '
NR==1 { name=$2}
NR==3 { dateformat=$9 OFS $6 OFS $8 OFS $7 OFS $10}
NR==4 { length=$8}
NR==5 { progress=int($3) }
END{print name,dateformat,length,progress};
' ) )
echo ${val_arr[0]}
echo ${val_arr[1]}
echo ${val_arr[2]}
echo ${val_arr[3]}
Output:
Testtool2
16:44:51
Fri
4
But the Output should look like this:
Testtool2
16:44:51 Fri 4 Jan 2013
2h0m
13
It looks like that print split the variable dateformat and give it to the Array.
That mean that the array looks like this:
val_arr[0] = Testtool2
val_arr[1] = 16:44:51
val_arr[2] = Fri
val_arr[3] = 4
val_arr[4] = Jan
val_arr[5] = 2013
Somehow how could help?
Greetz demonking
rdrtx1
January 4, 2013, 11:03am
2
try:
read arr <<< $(awk '
NR==1 {name=$2}
NR==3 {dateformat=$9 FS $6 FS $8 FS $7 FS $10}
NR==4 {lngth=$8}
NR==5 {progress=int($3)
}
END {print name, dateformat, lngth, progress}
' OFS="|" myfile)
set -- "$arr"
IFS="|"; declare -a val_arr=($*)
echo "${val_arr[0]}"
echo "${val_arr[1]}"
echo "${val_arr[2]}"
echo "${val_arr[3]}"
Hello rdrtx1 ,
thanks for your repley.
I get an error:
awk: syntax error at source line 4
context is
NR==4 { >>> length= <<< $8 }
awk: illegal statement at source line 5
Why is there this error?
Edit: Is there an other possiblity ?
I mean to convert the NR==3 expression to one string without splitting it .
Because i need it only in this format i has written in the first post
RudiC
January 5, 2013, 8:37am
4
I presume you are using bash or similar. bash seems to make a difference between assigning a list of space separated strings to an array variable and assigning the result of a command substitution of same format. Me too, I could not get it to work, other than using the workaround
var=( $(awk '
NR==1 { name=$1}
NR==3 { dat=$4 "_" $1 "_" $3 "_" $2 "_" $6}
NR==4 { leng=$1}
NR==5 { progress=int($1) }
END{print name, dat, leng, progress};
' file ) )
echo ${var[1]}
13:55:17_Sat_5_Jan_2013
BTW - the error is originating from you using the reserved word length
(function in awk)
After a lot of fiddling around - try this one:
$ eval var=( $(awk '
NR==1 { name=$1}
NR==3 { dat=$4 " " $1 " " $3 " " $2 " " $6}
NR==4 { leng=$1}
NR==5 { progress=int($1) }
END {print "\""name"\" \"" dat"\" \""leng"\" \""progress"\""};
' file ) )
$ echo ${var[1]}
13:55:17 Sat 5 Jan 2013
1 Like
shame over me
Thanks for your help RudiC.
Now it works
RudiC
January 5, 2013, 9:23am
6
Welcome - and don't feel ashamed. Everyone has his or her blind spots/blind moments.
rdrtx1
January 5, 2013, 10:40am
7
on the error mentioned above: do not use length
as a variable name. If is defined as a function.