7

I have csv data like:

a,b,c,d,e,f,g,h

I want this data in a list, one letter in each line. Is it possible is shell script? I have tried to extract each letter using cut command as shown below:

less list.txt | cut -d"," -f?

The command is working but the problem here is the number of fields is unknown. I have to place this command in a loop and iterate on the list so that I can extract the values one-by-one a redirect them to another file.

2
  • 1
    You should post some code and point to with what you are have problems, this will make it a lot easier for other to help you solve it. Commented Jun 26, 2015 at 11:00
  • 2
    Side point: it's odd to pipe your file into your command using less. More conventional would be cut file, cut < file, or, if you really want to use a pipe, cat file | cut. If less did its usual job, you'd have to keep hitting the spacebar to finish, but actually, it notices that standard output is not your screen, and basically turns itself into cat for you. Commented Jun 26, 2015 at 11:55

4 Answers 4

18

Use tr to change , into newlines:

tr , "\n" < list.txt

See https://en.wikipedia.org/wiki/Tr_(Unix)

Sign up to request clarification or add additional context in comments.

1 Comment

4

You could use the tr command to transform each "," into a newline.

cat list.txt | tr "," "\n"

From then you can output each line wherever you want using a while read loop

cat list.txt | tr "," "\n" | while read LINE
 do
  echo "$LINE" >> ~/wherever/you/want
done

Or ...

while read LINE
 do
  echo "$LINE" >> ~/wherever/you/want
done <<< "$(cat list.txt | tr "," "\n")"

Either works.

Comments

3

You can use sed for this:

sed "s/,/\n/g" list.txt

Output

a
b
c
d
e
f

5 Comments

Yes, I know. But it doesn't do harm, does it?
neither does writing sed -r -s -u -l10000 --posix -e "s/,/\n/g". Still pointless though.
The value in using -e is that if the sed script ever needs to start with a - (which may or may not actually be possible) you won't need to remember to add -e and if you ever want to add a second script to the command you can just add -e <script> to the end without needing to go add -e before the first script. That said it doesn't really matter.
@EtanReisner I'm pretty sure it would be a syntax error if you started with a -. My point was that it is a single script that doesn't begin like that. I understand when -e can be useful, just like all the flags in my other comment "can" be useful, just not in this case.
@EtanReisner Yes, because that is it's purpose, obviously the other flags aren't going to do that, they are all useful though in situations that demand them. It's like saying -r should be there so you don't have to remember to add it if you start using ERE. But OP is not and neither of them is needed.
2

Yes, you can use cut. The key point is the usage of --output-delimiter. If you set it as new line, everything works:

cut -d',' --output-delimiter=$'\n' -f1- file

Note also two facts:

  • we use -f1- to print from the first field up to the last one. The n- syntax means: from the n-th field up to the end.
  • we use $'\n' because \n alone would print literal \n instead of real new lines.

Test

$ echo "a,b,c,d,e,f,g,h" | cut -d',' --output-delimiter=$'\n' -f1-
a
b
c
d
e
f
g
h

From man cut:

--output-delimiter=STRING

use STRING as the output delimiter the default is to use the input delimiter

2 Comments

@Armfoot thanks, even though it is something I am already posting in my answer. That is, I don't see what value this is adding. But thanks for your support
ah... it's just a real test/fiddle/confirmation is much nicer to the one who's going to use it ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.