Concate a string in awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concate a string in awk
# 1  
Old 01-04-2013
Concate a string in awk

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]}



But the Output should look like this:

16:44:51 Fri 4 Jan 2013

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

Last edited by demonking; 01-05-2013 at 09:52 AM..
# 2  
Old 01-04-2013
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]}"

Last edited by rdrtx1; 01-04-2013 at 06:59 PM..
# 3  
Old 01-05-2013
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

Last edited by demonking; 01-05-2013 at 10:01 AM..
# 4  
Old 01-05-2013
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]}

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

Last edited by RudiC; 01-05-2013 at 10:56 AM.. Reason: Solution with eval...
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-05-2013
shame over me Smilie

Thanks for your help RudiC.

Now it works
# 6  
Old 01-05-2013
Welcome - and don't feel ashamed. Everyone has his or her blind spots/blind moments.
# 7  
Old 01-05-2013
on the error mentioned above: do not use length as a variable name. If is defined as a function.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

3. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

4. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

7. Shell Programming and Scripting

String concate problem

Hello there, I am facing the following problem in string concate. I am returning a sql column value to a shell script variable . Now when I am trying to concate the variable with another string (string appears first) I am getting a space between the two concate. Example p is the shell... (2 Replies)
Discussion started by: Pratik4891
2 Replies

8. UNIX for Dummies Questions & Answers

using nawk to concate the fields

I have the the following question: # cat report.lis | nawk -F: '{ if($0 ~ "^BE NO:") print "report_"$2".lis"}' :confused: .lisrt_ 111 .lisrt_ 111 .lisrt_ 222 .lisrt_ 222 .lisrt_ 333 .lisrt_ 333 # cat report.lis | nawk -F: '{ if($0 ~ "^BE NO:") print "report_" $2}' report_ 111 report_... (2 Replies)
Discussion started by: raychu65
2 Replies

9. Shell Programming and Scripting

concate the record

how i can bring this file into three records in my sample text file there are thre records new records always start with Data File ================================================== samle file ================================================= Data File:... (7 Replies)
Discussion started by: aboorkuma
7 Replies

10. Shell Programming and Scripting

Concate all lines into one.

I have a file with 1000 records and I have to produce another file with 1 line which is a concatentation of all these records. Please help. (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question

Featured Tech Videos