How to append or prepend text in specific columns with awk
Assume the following scenario, you have a csv file with the following content and you need to do append / prepend text in specific columns…
Assume the following scenario, you have a csv file with the following content and you need to do append / prepend text in specific columns, doing this with awk is very easy as you will see! lets start
How to append text
Assume that we have the following file as example.csv and we want to append text “test” to all values of second column except the headers
name,age,gender
kostas,43,male
Nikos,42,male
Maria,35,femaleTo do this enter the following one-liner
awk 'BEGIN{OFS=FS=","} NR==1 {print} NR>1 {$2=$2 "_test"; print}' example.csv
name,age,gender
kostas,43_test,male
Nikos,42_test,male
Maria,35_test,femaleLets break down how it works
BEGIN{OFS=FS=","}we define comma as delimiter between columnsNR==1 {print}if number of record is the first one just print itNR>1 {$2=$2 "_test"; print}if number of record is the the header, the first one then, for each second value of the row append text “_test”, this translates to set column two to column two + “_test”
How to prepend text
Now lets see another example, how to prepend text to a column, to do this enter the following
awk 'BEGIN{OFS=FS=","} NR==1 {print} NR>1 {$1="test_"$1; print}' example.csv
name,age,gender
test_kostas,43,male
test_Nikos,42,male
test_Maria,35,femaleIt isn’t much different than the previous example except that now we do this $1="test_"$1 which translates to set column one as “test_” + column one
How to append and prepend at the same time
Now lets see how we can append and prepend at the same time, enter the following
awk 'BEGIN{OFS=FS=","} NR==1 {print} NR>1 {$1="test_"$1"_test"; print}' example.csv
name,age,gender
test_kostas_test,43,male
test_Nikos_test,42,male
test_Maria_test,35,femaleAs you can see is quite easy, $1="test_"$1"_test" which translates to set column1 to “test_” + column one + “_test”.
How to perform operations in multiple columns
Now lets see something more advanced, how we can append / prepend text to multiple columns, lets prepend and append “test” to first and last column
awk 'BEGIN{OFS=FS=","} NR==1 {print} NR>1 {$1="test_"$1;$3=$3"_test";print}' example.csv
name,age,gender
test_kostas,43,male_test
test_Nikos,42,male_test
test_Maria,35,female_test$1="test_"$1 prepends text to first column
$2=$2"_test" appends text to last column
How to append / prepend text to last column with a file that has an unknown number of columns
What if the file we work has an unknown number of columns or the number of columns can vary per row and still we want to append / prepend text to the last column? quite easy, assume that we have the following csv
name,age,gender,has_only_fans
kostas,43,male
Nikos,42,male
Maria,35,female
ilias,44,male,yesNote that the column named has_only_fans has values only in the last row but we still want to append text to the last column of each row despite the un-equal nunber of columns, we can $NF refers always to last column of the row
awk 'BEGIN{OFS=FS=","} NR==1 {print} NR>1 {$NF=$NF"_test";print}' example.csv
name,age,gender,has_only_fans
kostas,43,male_test
Nikos,42,male_test
Maria,35,female_test
ilias,44,male,yes_testConclusion
Knowing to work with awk can make you life much easier! of course you can do everything with Python, but still awk exists by default in every system and its one-liners are super easy to learn!