SonicProtein
SonicProtein

Reputation: 850

Using cut multiple times and concatenating the results using multiple delimiters?

If I have a file:

c1 c2 c3 c4 c5 c6
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18 

And I only want the first, second, fourth and fifth columns into one line but separated by different delimiters..

For example line 1 would be: 1;2:4-5 Line 2 would be: 7;8:10-11 Line 3 would be: 13;14:16-17

I think with the same delimiter the command would be something like:

paste --delimiter=':' <(cut -f1 file.txt) <(cut-f2 file.txt) <(cut -f4 file.txt) <(cut -f5 file.txt)

The result should be in an array such that each line is a separate entry in the array

IFS='\n'; echo "${array[*]}"

1;2:4-5
7;8:10-11
13;14:16-17

I'm thinking awk might be able to achieve this but I'm unable to come up with it...

Upvotes: 0

Views: 4921

Answers (4)

agc
agc

Reputation: 8406

Shell script:

while read x ; do set - $x ; echo "$1;$2:$4-$5" ; done < filename
1;2:4-5
7;8:10-11
13;14:16-17

Upvotes: 0

clt60
clt60

Reputation: 63912

With perl:

perl -lanE 'printf "%s;%s:%s-%s\n",@F[0,1,3,4]' file

prints

c1;c2:c4-c5
1;2:4-5
7;8:10-11
13;14:16-17

skipping header

perl -lanE 'printf "%s;%s:%s-%s\n",@F[0,1,3,4] if $.>1'

Upvotes: 1

kaho
kaho

Reputation: 4784

I guess you can use sed if you alway has 4 fields

line="1 2 3 4 5 6
> 7 8 9 10 11 12
> 13 14 15 16 17 18 "
echo "$line" | cut -f1,2,4,5 -d " " | sed 's/\([^ ]*\) \([^ ]*\) \([^ ]*\) \(.*\)/\1\;\2\:\3\-\4/g '

Upvotes: 1

Wintermute
Wintermute

Reputation: 44043

To make awk print each line like you specified, use

awk '{ print $1 ";" $2 ":" $4 "-" $5 }' filename

Or, to exclude the header line,

awk 'NR > 1 { print $1 ";" $2 ":" $4 "-" $5 }' filename

To get the results of that into a bash array:

while IFS='\n' read line; do array+=("$line"); done < <(awk '{ print $1 ";" $2 ":" $4 "-" $5 }' filename)

Upvotes: 3

Related Questions