Dima Kuzmin
Dima Kuzmin

Reputation: 161

How to convert a string from a template?

There are lines:

echo "1111111111111; 2222222222; 8; 2; 34,12,2,3,5,1; UNKNOW;"

or

echo "1111111111111; 2222222222; 8; 2; 34,12,2,3,5,1,5,12,45,34; UNKNOW;"

or

echo "1111111111111; 2222222222; 8; 2; 3; UNKNOW;"

How to take the value of the last character ";" and to the last comma ","?

Upvotes: 0

Views: 64

Answers (2)

Ronak Patel
Ronak Patel

Reputation: 3849

With awk: -F param is for field delimiter/separator, $5 - fifth field separated by delimited defined in -F param:

$ echo "1111111111111;2222222222;8;2;3,1;UNKNOW;" | awk -F';' '{print $5}'
3,1

With cut: -d param is for field delimiter/separator, -f5 - fifth field separated by delimited defined in -d param:

$ echo "1111111111111;2222222222;8;2;3,1;UNKNOW;" | cut -d ';' -f5
3,1

Out of context but you can use this to get some chars from string: -c1,2,3,25-30 - cut the chars 1,2,3,25,26,27,28,29,30

echo "1111111111111;2222222222;8;2;3,1;UNKNOW;" | cut -c1,2,3,25-30
111;8;2;3

Upvotes: 1

Walter A
Walter A

Reputation: 19982

In the comment you showed a solution that satifies you retrieving the 5th field. That is also possible with

echo "${line}" | cut -d ";" -f5

Upvotes: 1

Related Questions