Question

Can this be achieved using awk? I am looking to add a new column which has delimited data from an existing column. My input file is a tab delimited text file. The column with device-0, device-1 is the new column derived from phone-0-1, phone-1-2.

Input:

category1    phone-0-1   working     0000   0000     new     0
category1    phone-1-2   working     0000   0000     new     0
category1    phone-2-4   working     0000   0000     new     0
category1    phone-3-5   working     0000   0000     new     0
category1    phone-4-6   working     0000   0000     new     0

Output:

category1    device-0   phone-0-1    working     0000   0000     new     0
category1    device-1   phone-1-2    working     0000   0000     new     0
category1    device-2   phone-2-4    working     0000   0000     new     0
category1    device-3   phone-3-5    working     0000   0000     new     0
category1    device-4   phone-4-6    working     0000   0000     new     0
Was it helpful?

Solution

try this sed line, see if it works:

 sed 's/\sphone-[0-9]\+/&\t&/' file

same idea with awk:

awk -F'\t' -v OFS='\t' '{sub(/phone-[0-9]+/,"&\t&",$2)}7' file

EDIT, rename to device:

sed 's/\(\s\+\)phone-\([0-9]\+\)/\1device-\2&/' file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top