Leopoldo
Leopoldo

Reputation: 825

how to use sub-string with awk

I am using following line to write some data to the file:

cat $srcfile | awk -v p=$prefix '{printf("XX_%s%s XX_%s\n", p, $1, $1);}' > $dstfile

How can I change XX by two first letters of $1?

Upvotes: 0

Views: 49

Answers (1)

Cyrus
Cyrus

Reputation: 88553

With substr:

echo "abcdef 12345" | awk -v p="foo" '{x=substr($1,1,2); printf("%s_%s%s %s_%s\n", x, p, $1, x, $1);}'

Output:

ab_fooabcdef ab_abcdef

Upvotes: 2

Related Questions