简体   繁体   中英

how to print each word in each line using linux terminal?

suppose there is a file like this..(6 Ids)

k0012, k0013

k40035, k3089

Glc, 3-PGA

I want to print each Ids in each line using linux terminal. thanks in advance

You can use tr:

tr ' ,' '\0\n' file

Or gnu-sed:

sed 's/, */\n/g' <file

Or gnu awk:

awk -F, '{print $1}' RS='[[:space:]]+' file

Using grep :

$ grep -oP "\b[^\s,]*\b" inputfile
k0012
k0013
k40035
k3089
Glc
3-PGA

From your "input" it is unclear if the IDs are separated by blanks or by blanks and commas. If the latter is true, you probably need to handle both:

tr ' ,' '\n\n' < myfile | uniq

(you'll need uniq to remove those excessive blank lines, although you might probably be better off by using awk for it):

tr ' ,' '\n\n' < myfile | awk 'NF > 0'

Are you after this? Using this input: (6 Ids) k0012, k0013 k40035, k3089 Glc, 3-PGA

$ awk -F'[, ]*' '{for(i=3;i<=NF;i++)print $i}' input
k0012
k0013
k40035
k3089
Glc
3-PGA

If the input instead is this:

k0012, k0013

k40035, k3089

Glc, 3-PGA

try:

$ awk -F'[, ]*' '{for(i=1;i<=NF;i++)print $i}' input
k0012
k0013
k40035
k3089
Glc
3-PGA

...or of there's only 2 columns:

awk -F'[, ]*' '{printf("%s\n%s", $1, $2)}' input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM