Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new line for third data 1

Status
Not open for further replies.

dbase77

Technical User
Apr 23, 2002
591
IE
Hi,

Any tools I can use like sed or awk in order to achieve below:

24165B,0000007912,0076994151

24197M,0000009523,20376530

24692A,0000013303,0055280351

25121F,0000016000,044637551

26266H,0000008000,204201199

I want the third data go to next line. Example:

24165B,0000007912
0076994151

24197M,0000009523
20376530

24692A,0000013303
0055280351

25121F,0000016000
044637551

26266H,0000008000
204201199

Thank you in advance.

dbase77

 
Something like this ?
awk -F"," 'NF==3{printf "%s,%s\n%s\n",$1,$2,$3;next}1' /path/to/input >output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

I got syntax error if I put 1 at the end. But if I delete 1 at the end, it worked. Can you tell me how this awk thing works?

If I need to put extra space, what can I do to achieve this using above code? Example

24692A,0000013303
0055280351spacespacespace

25121F,0000016000
044637551spacespacespace

26266H,0000008000
204201199spacespacespace

Thanks again.

dbase77

 
Replace this:
"%s,%s\n%s\n",
By this:
"%s,%s\n%s \n",

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Good stuff. I like the outcome now. But some minor problem with the application that I use require the space to have same length. The output for example:

first line: 24165B,0000007912
second line:0076994151xxxxxxx

first line: 24197M,0000009523
second line:20376530xxxxxxxxx

first line: 24692A,0000013303
second line:0055280351xxxxxxx

Any good way I could add spaces to match number of digits on first line? Example the first group has 7 spaces to add and second group has 9 spaces and third group has 7 spaces. All these groups are vary in length.

Thanks again for valuable help.

dbase77
 
Try something like this:
awk -F"," '
NF==3{
s1=sprintf("%s,%s",$1,$2)
printf "%s\n%-"length(s1)"s\n",s1,$3
next
}
{print}
' /path/to/input >output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
wow, this is very good. Are you a unix guru or something. Do you mind explaining what each line does, please? I'm trying to learn and might use it for other stuff. I forgot to mention that I need to put spaces as well for third line. Example:

first line: 24165B,0000007912
second line:0076994151xxxxxxx
third line:xxxxxxxxxxxxxx
first line: 24197M,0000009523
second line:20376530xxxxxxxxx
third line:xxxxxxxxxxxxxx
first line: 24692A,0000013303
second line:0055280351xxxxxxx
third line:xxxxxxxxxxxxxx

The third line space length should be the same as first line length. If you can explain the code above, I could figure it out myself.

Thanks again.

dbase77

 
Hi,

Got it. I be able to figure out where to put the extra spaces.

Thank you again.

dbase77
 
Try this:
awk -F"," 'NF==3{
s1=sprintf("%s,%s",$1,$2)
l=length(s1)
printf "%s\n%-"l"s\n%"l"s\n",s1,$3," "
}' /path/to/input >output
And now, the explainations.
awk -F"," 'NF==3{
launch awk with comma as field separator and deal with record with exactly 3 fields
s1=sprintf("%s,%s",$1,$2)
store in a variable named s1 the first 2 fields values separated by a comma
l=length(s1)
store in a variable named l the length of the s1 variable value
printf "%s\n%-"l"s\n%"l"s\n",s1,$3," "
print 3 lines, s1 in the 1st, $3 padded with spaces to reach s1's length and finally l spaces

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You got even better coding. This is what I use:

awk -F"," '
NF==3{
s1=sprintf("%s,%s,",$1,$2)
printf "%s\n%-"length(s1)"s\n ",s1,$3
next
}
{print}
' /path/to/input >output

Thank you for explanation for each line of code.

dbase77
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top