user1488639
user1488639

Reputation: 115

Split a text file based on delimiter using linux command line

I have a file with the following lines of text:

jeremy , thomas , 123 
peter , paul , 456 
jack , jill , 789

I would like to remove all of the data except for the center item. For example ending up with a file which contains:

thomas
paul
jill

I have tried so many awk patterns my brain is exploding. Any help would be appreciated.

Upvotes: 4

Views: 15203

Answers (4)

azar
azar

Reputation: 1

Try this:

$ cat (your file)| cut -d ',' -f2 > (new file)

for instance:

$ cat /home/file1.txt | cut -d ',' -f2 > /home/file2.txt

Upvotes: 0

Kent
Kent

Reputation: 195029

grep look around:

grep -Po '(?<=, ).*(?= ,)' file

Upvotes: 1

kev
kev

Reputation: 161604

Try awk:

awk -F '[[:space:]]*,[[:space:]]*' '{print $2}' input.txt

Upvotes: 7

Necrolyte2
Necrolyte2

Reputation: 748

Try this

cat <filepath> | tr -d ' ' | cut -d',' -f2

Upvotes: 1

Related Questions