Need help on an old post - How to convert a comma delimited string to records or lines of text?

Hi,

Apologies in advance to the moderator if I am posting this the wrong way.

I've searched and found the solution to an old post but as it is a very old post, I don't see an option to update it with additional question.

The question I have is in relation to the following post:

How to convert a comma delimited string to records or lines of text?

I just want to know if there is a way to number the output. At the moment, I am piping the output to nl to generate a numbered list. I just want to know how I can change the following so it can be a numbered output without using nl.

Not sure if I should PM RudiC or RavinderSingh13, sorry.

tr ',' '\n' < file3
user1
user2
user3
user4

awk -F"," '{for(i=1;i<=NF;i++){print $i}}' Input_file
OR
awk '{gsub(/\,/,"\n",$0);print $0}' Input_file

No, I think you did the right thing. You searched the forum for your problem, and now you are asking for a refinement, after finding your own solution, which you consider sub-optimal. I have to admit I would be happy with piping the output into nl .

Try this variation on your second solution:

$ printf "a,b,c,d\ne,f,g,h\n" | awk -F"," 'BEGIN{x=1;}{for(i=1;i<=NF;i++){print x++,$i}}'
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h

Andrew

2 Likes

Thanks Andrew.
Yeah, the nl works fine . Just curious if there is an option to not use it.
Had tried your suggestion and that works fine.
Found it by chance from Googling that I can actually do RavinderSingh13's to just do

awk -F"," '{for(i=1;i<=NF;i++){print i " " $i}}' Input_file

Always room for learning. Now, I am curious how to do that in the gsub :slight_smile:

1 Like

Hello newbie_01,

As per forum rules we should NOT ask questions in PMs. Good that you found answer by searching it, for future it is always good to mention 3 things. 1- Sample of your Input_file, 2- sample of your expected Output and 3rd- Most important one, what have you have tried in order to solve the problem.

Once you provide all details you will get all kind of guidance needed on here, keep learning and keep sharing on this great site,cheers.

Thanks,
R. Singh

1 Like

How about

awk '{print NR, $0}' RS="," file
1 user1
2 user2
3 user3
4 user4

 

If your input has several lines, try RS="[,\n]" - if your awk version allows for regex record separators.

1 Like

use 'cat -n' on the tailing pipe if you want to avoid funky awk ...

[book@room ~]$ echo 'a,b,c,d' | tr ',' '\n' | cat -n
     1  a
     2  b
     3  c
     4  d
[book@room ~]$

Standard (POSIX) cat doesn't have any -n switch. This is because it is for conCATenating files. It is neither a display vehicle, nor a make-a-datastream-out-of-my-file-utility, fullscreen editor or anything else than a file concatenating tool.

Rob Pike and Brian Kernighan wrote a paper together ("cat -v considered harmful", Usenix conference 1983 if memory serves correctly) which later was the basis for the book "Programming in the UNIX environment" by the same authors. It should be a set book for every beginner in the UNIX world.

Here is a standard-conforming way of numbering lines, using sed . Interestingly enough i noticed that all three invocations of sed are necessary, it won't work with two:

echo 'a,b,c,d' | sed ':start;/,/ {h;s/,.*//p;g;s/^[^,]*,//;b start;}' | sed = | sed 'N;s/\n/   /'

I hope this helps.

bakunin

2 Likes