Extract text between two slashes and add it at the end of the line after a tab character

0

I have a text file looking like this:

data/CON/001/raw_series_ST_MC_REG_SM.nii
data/PAT/001/raw_series_ST_MC_REG_SM.nii

I would like to achieve something like this:

data/CON/001/raw_series_ST_MC_REG_SM.nii TAB CON
data/PAT/001/raw_series_ST_MC_REG_SM.nii TAB PAT

Of course tab in the means of space.

Thx for the help!

Jan

Posted 2014-10-01T09:14:29.120

Reputation: 1

Answers

0

Here are a few choices:

awk '{split($0, a, /\//); print $0 "\t" a[2]}' file
awk -F/ -v OFS="\t" '{print $0, $2}' file

sed -r 's#^([^/]*/([^/]*)/.*)#\1\t\2#' file
sed 's#^\([^/]*/\([^/]*\)/.*\)#\1\t\2#' file
sed 'p; s#[^/]*/##; s#/.*##' file | paste - -

perl -F/ -lane 'print "$_\t$F[1]"' file

glenn jackman

Posted 2014-10-01T09:14:29.120

Reputation: 18 546