RhysJ
RhysJ

Reputation: 173

In Linux command line console, how to get the sub-string from a file?

The content of the file is fixed.

Example:

2016-03-28T00:02 AAA 2016-03-28T00:03  ADASDASD
2016-03-28T00:03 BBB 2016-03-28T00:04  FAFAFDAS
2016-03-28T00:05 CCC 2016-03-28T00:06  SDAFAFAS
....

Which command can I use to get all sub-strings, AAA, BBB, CCC, etc.

Upvotes: 0

Views: 155

Answers (3)

OmPS
OmPS

Reputation: 341

you can use cut and awk and perl for this.

cat >> file.data << EOF
2016-03-28T00:02 AAA 2016-03-28T00:03  ADASDASD
2016-03-28T00:03 BBB 2016-03-28T00:04  FAFAFDAS
2016-03-28T00:05 CCC 2016-03-28T00:06  SDAFAFAS
EOF

AWK

awk '{ print $2 }' file.data
AAA
BBB
CCC

CUT

cut -d " " -f2 file.data
AAA
BBB
CCC

PERL

perl -alne 'print $F[1] ' file.data 
AAA
BBB
CCC

Upvotes: 2

birdoftheday
birdoftheday

Reputation: 914

You can use AWK for this:

jayforsythe$ cat > file
2016-03-28T00:02 AAA 2016-03-28T00:03 ADASDASD
2016-03-28T00:03 BBB 2016-03-28T00:04 FAFAFDAS
2016-03-28T00:05 CCC 2016-03-28T00:06 SDAFAFAS
jayforsythe$ awk '{ print $2 }' file
AAA
BBB
CCC

To save the result to another file, simply add the redirection operator:

jayforsythe$ awk '{ print $2 }' file > file2

Upvotes: 0

demosito
demosito

Reputation: 233

You can use cut:

cut -d' ' -f 2 file

Upvotes: 0

Related Questions