94

In awk, I have 2 fields: $1 and $2.

They are both strings that I want to concatenate and assign to a variable.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user3738926
  • 1,178
  • 1
  • 10
  • 17

4 Answers4

142

Just use var = var1 var2 and it will automatically concatenate the vars var1 and var2:

awk '{new_var=$1$2; print new_var}' file

You can put an space in between with:

awk '{new_var=$1" "$2; print new_var}' file

Which in fact is the same as using FS, because it defaults to the space:

awk '{new_var=$1 FS $2; print new_var}' file

Test

$ cat file
hello how are you
i am fine
$ awk '{new_var=$1$2; print new_var}' file
hellohow
iam
$ awk '{new_var=$1 FS $2; print new_var}' file
hello how
i am

You can play around with it in ideone: http://ideone.com/4u2Aip

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 9
    Or just `awk '{print $1$2}' file` – Jonathan Jul 28 '17 at 08:07
  • how do you concatenate named variable say a='aaa'; b='bbb' you, can manipulate the OFS variable. but how about if you want to print out other fields with the regular OSF="\t"; Is the printf the best choice? – Kemin Zhou Feb 15 '18 at 00:31
  • @KeminZhou not very sure about what you mean, but it looks like [`sprintf()`](https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html) may help you. – fedorqui Feb 15 '18 at 07:28
  • 2
    ...and for those of us who have yet to understand when to use `$` for variables, don't use it even for custom intermediate variables on the right hand side of an assignment (unlike builtin `$1` etc) – Sridhar Sarnobat Jun 30 '22 at 03:12
20

Could use sprintf to accomplish this:

awk '{str = sprintf("%s %s", $1, $2)} END {print str}' file
kvantour
  • 25,269
  • 4
  • 47
  • 72
Hongbo Liu
  • 2,818
  • 1
  • 24
  • 18
  • That was the perfect solution for concatenating my string in a for loop as `pwd | awk -F/ '{for (i=1; i – aerijman Dec 19 '19 at 13:22
6

You can also concatenate strings from across multiple lines with whitespaces.

$ cat file.txt
apple 10
oranges 22
grapes 7

Example 1:

awk '{aggr=aggr " " $2} END {print aggr}' file.txt
10 22 7

Example 2:

awk '{aggr=aggr ", " $1 ":" $2} END {print aggr}' file.txt
, apple:10, oranges:22, grapes:7
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Piyush Bansal
  • 101
  • 1
  • 4
5

Concatenating strings in awk can be accomplished by the print command AWK manual page, and you can do complicated combination. Here I was trying to change the 16 char to A and used string concatenation:

echo    CTCTCTGAAATCACTGAGCAGGAGAAAGATT | awk -v w=15 -v BA=A '{OFS=""; print substr($0, 1, w), BA, substr($0,w+2)}'
Output: CTCTCTGAAATCACTAAGCAGGAGAAAGATT

I used the substr function to extract a portion of the input (STDIN). I passed some external parameters (here I am using hard-coded values) that are usually shell variable. In the context of shell programming, you can write -v w=$width -v BA=$my_charval. The key is the OFS which stands for Output Field Separate in awk. Print function take a list of values and write them to the STDOUT and glue them with the OFS. This is analogous to the perl join function.

It looks that in awk, string can be concatenated by printing variable next to each other:

echo xxx | awk -v a="aaa" -v b="bbb" '{ print a b $1 "string literal"}'
# will produce: aaabbbxxxstring literal
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56