Split a text file based on delimiter using linux command line

awk
14,689

Solution 1

Try awk:

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

Solution 2

Try this

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

Solution 3

grep look around:

grep -Po '(?<=, ).*(?= ,)' file
Share:
14,689
user1488639
Author by

user1488639

Updated on June 12, 2022

Comments

  • user1488639
    user1488639 almost 2 years

    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.