Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Example: I'd like to split $2 in input file, putting a tab after pattern surname

Input:

name    surname1    
name    surname30000
name    surname456

Desired output:

name    surname    1
name    surname    30000
name    surname    456
share|improve this question

4 Answers 4

up vote 2 down vote accepted

An awk alternative:

awk 'match($2,/[0-9]+$/) {printf("%s\t%s\t%s\n", $1, substr($2,0,RSTART), substr($2,RSTART,RLENGTH))}' filename

This will match only the numeric string at the end of the second column.

share|improve this answer

You can lookup the first digit and replace it by a tab followed by the digit using:

sed 's/[0-9]/\t&/' file
share|improve this answer
sed -e 's/\([0-9]\)/\t\1/' input.txt

adds a tab before the first digit on every line.

Output:

name    surname 1    
name    surname 30000
name    surname 456
share|improve this answer

The data might be in a specific column, e.g., 16:

sed -e 's/^\(.\{15,15\}\)/\1 /'

Alternatively, the data of interest in the third field may not be numeric:

sed -e 's/^\([[:alpha:]]\+[[:space:]]\+\)\([[:alpha:]]\+\)/\1\2 /'

Either of these will put a space before the digits (or other non-alphabetic) characters in the data.

For reference:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.