INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

create new lines

create new lines

(OP)
Hi
I would like to add new lines with the strings after the comma.
The lines in the inFile have 2 fields separated by a tab.
I would like to remove the comma from the 2d fileld and add a new line where the 1st filed is empty and the 2d field is made of the string that followed the comma.

inFile
A    m,n
B    x,y,z
C    t,r


outFile
A    m
      n
B    x
      y    
      z
C    t
      r


Thank you for helping
Joseph

RE: create new lines

CODE

tmp# cat txt
A  m,n
B  x,y,z
C  t,r

tmp# cat txt | awk '{gsub(",","\n");print}' |sed 's/^[a-z]/   &/'
A  m
   n
B  x
   y
   z
C  t
   r

RE: create new lines

When I tried it without using sed the output was

CODE

# awk '{gsub(",","\n");print}' txt
A m
n
B x
y
z
C t
r

Wasn't sure if the lines after the commas were supposed to be indented or not, so added the pipe to indent.

RE: create new lines

After looking at your post again, I see the spaces you added in awk after the newline which I didn't catch before!   
 

RE: create new lines

(OP)
thank you everybody. It was very helpful.
I added a tab to get the format I want
awk '{gsub(",","\n\t");print}' inFile.txt
A    m
     n
B    x
     y
     z
C    t
     r


joseph

RE: create new lines

(OP)
how can I get the reverse format?
For the lines that have the same first field, I want to reduce them to one line having the second field strings separated by a comma.

inFile

A    x
B    y
B    z
C    r
C    s
C    t
D    q

outFile

A    x
B    y,z
C    r,s,t
D    q

thank you
joseph

RE: create new lines

(OP)
thank you so much, PHV

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close