Smith Black
Smith Black

Reputation: 535

Substring in UNIX

Suppose I have a string "123456789".

I want to extract the 3rd, 6th, and 8th element. I guess I can use

cut -3, -6, -8

But if this gives

368

Suppose I want to separate them by a white space to get

3 6 8

What should I do?

Upvotes: 4

Views: 1701

Answers (4)

devnull
devnull

Reputation: 123698

Using cut:

$ cat input
1234567890
2345678901
3456789012
4567890123
5678901234
$ cut -b3,6,8 --output-delimiter=" " input
3 6 8
4 7 9
5 8 0
6 9 1
7 0 2

The -b option selects only the specified bytes. The output delimiter can be specified using --output-delimiter.

Upvotes: 1

hwnd
hwnd

Reputation: 70750

Perl one-liner.

perl -lne '@A = split //; print "$A[2] $A[5] $A[7]"' file

Upvotes: 1

kojiro
kojiro

Reputation: 77197

Actually shell parameter expansion lets you do substring slicing directly, so you could just do:

x='123456789'
echo "${x:3:1}" "${x:6:1}" "${x:8:1}"

Update

To do this over an entire file, read the line in a loop:

while read x; do
  echo "${x:3:1}" "${x:6:1}" "${x:8:1}"
done < file

(By the way, bash slicing is zero-indexed, so if you want the numbers '3', '6' and '8' you'd really want ${x:2:1} ${x:5:1} and {$x:7:1}.)

Upvotes: 2

CSᵠ
CSᵠ

Reputation: 10169

You can use the sed tool and issue this command in your teminal:

sed -r "s/^..(.)..(.).(.).*$/\1 \2 \3/"

Explained RegEx: http://regex101.com/r/fH7zW6


To "generalize" this on a file you can pipe it after a cat like so:

cat file.txt|sed -r "s/^..(.)..(.).(.).*$/\1 \2 \3/"

Upvotes: 1

Related Questions