E.Cross
E.Cross

Reputation: 2137

Prepending text to a specific column in a tab-delimited file?

I have a file like this (tab delimited), but many lines

1314    0   0   0   0   0   3   1321    -   k63_1878003 1314    0   1314    6   171115067   64288422    64291057    4   12,131,75,1096, 0,12,143,218,   64288422,64288802,64289161,64289961,

I need to prepend a string to column 14 of each line but keep everything else the same. Can I do this in awk or would it be better in sed?

Upvotes: 3

Views: 8909

Answers (4)

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed 's/[^\t]*/string&/14' file

Upvotes: 1

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84453

You can use concatenation to append to a field in AWK. For example:

$ echo "foo bar baz" | awk -vOFS=$'\t' '$2 = "quux" $2'
foo quuxbar baz

In your case, you would obviously use field $13 instead. By making changes in the pattern portion, rather than the action, you are just replacing the value of the field and using the default print action for the line.

Upvotes: 0

Birei
Birei

Reputation: 36292

Using awk. OFS prints a tab between fields in output:

awk 'BEGIN { OFS = "\t" } { $14 = "string" $14; print }' infile

Upvotes: 12

vergenzt
vergenzt

Reputation: 10427

$ cat file.txt | sed 's/\(\([^\t]\+\t\)\{13\}\)/\1string/g'

In other words: replace (([^ ] +){13}) (thirteen non-tab chunks followed by tabs) with that same text, plus your string.

Upvotes: 1

Related Questions