Discussion:
[Help-bash] have the same column L
Val Krem
2017-07-14 01:29:45 UTC
Permalink
Hi all,

How can I starndarzied string column to have the same column length for each row.

Example
filename

A135953
D10036050
C135858000

I want add leading zeros and the column length should be 10

I tried
awk '{ printf "%010s \n", $1}' filename

Got all zeros
0000000000
0000000000
0000000000
0000000000

But I want
000A135953
0D10036050
C135858000


I would appreciate if you help me out.

thank you in advance
Seth David Schoen
2017-07-14 01:39:52 UTC
Permalink
Post by Val Krem
Hi all,
How can I starndarzied string column to have the same column length for each row.
Example
filename
A135953
D10036050
C135858000
I want add leading zeros and the column length should be 10
I'm not sure if this is GNU sed-specific, but you could use

sed -r ':x; /^.{,9}$/ s/^/0/; tx'
--
Seth David Schoen <***@loyalty.org> | No haiku patents
http://www.loyalty.org/~schoen/ | means I've no incentive to
8F08B027A5DB06ECF993B4660FD4F0CD2B11D2F9 | -- Don Marti
Dennis Williamson
2017-07-14 02:01:15 UTC
Permalink
On Jul 13, 2017 8:30 PM, "Val Krem" <***@yahoo.com> wrote:

Hi all,

How can I starndarzied string column to have the same column length for
each row.

Example
filename

A135953
D10036050
C135858000

I want add leading zeros and the column length should be 10

I tried
awk '{ printf "%010s \n", $1}' filename

Got all zeros
0000000000
0000000000
0000000000
0000000000

But I want
000A135953
0D10036050
C135858000


I would appreciate if you help me out.

thank you in advance


Your question is not about Bash so this is the wrong list.

However your AWK code works for me. You probably have something in column 1
in your file other than what you think you do.
Chris F.A. Johnson
2017-07-14 19:20:58 UTC
Permalink
Post by Val Krem
Hi all,
How can I starndarzied string column to have the same column length for each row.
Example
filename
A135953
D10036050
C135858000
I want add leading zeros and the column length should be 10
I tried
awk '{ printf "%010s \n", $1}' filename
awk '{ printf "%010d \n", $1}' filename
--
Chris F.A. Johnson, <http://cfajohnson.com>
Greg Wooledge
2017-07-14 19:35:55 UTC
Permalink
Post by Chris F.A. Johnson
Post by Val Krem
A135953
D10036050
C135858000
I want add leading zeros and the column length should be 10
I tried
awk '{ printf "%010s \n", $1}' filename
awk '{ printf "%010d \n", $1}' filename
Yeah, except his input isn't numeric. We don't know what it is, because
he won't TELL US.

$ echo "A135953" | awk '{ printf "%010d \n", $1}'
0000000000

Asking to zero-pad these non-numeric strings is completely nonsensical,
so I'm just waiting for a real explanation. I don't expect one to
come out. This person has a long history of asking ridiculous things
out of the blue. It all smells like homework.

Some other people on the list have guessed that perhaps the input
strings are hexadecimal, and have given printf commands that would
be appropriate for that case.
Val Krem
2017-07-18 00:32:27 UTC
Permalink
Hi all, Sorry for the confusion. I started using Linux environment recently. I found it very useful, and powerful. I am still learning.

The data that I am working on is ASCII file. One of the column of the file contains alphanumeric and I want each row of this column to have the same columns length.


Thank you for the help I got right what I wanted to do using this

cat filename |awk '{printf "%010s\n",$1}' | sed 's/ /0/g'
Post by Chris F.A. Johnson
Post by Val Krem
A135953
D10036050
C135858000
I want add leading zeros and the column length should be 10
I tried
awk '{ printf "%010s \n", $1}' filename
awk '{ printf "%010d \n", $1}' filename
Yeah, except his input isn't numeric. We don't know what it is, because
he won't TELL US.

$ echo "A135953" | awk '{ printf "%010d \n", $1}'
0000000000

Asking to zero-pad these non-numeric strings is completely nonsensical,
so I'm just waiting for a real explanation. I don't expect one to
come out. This person has a long history of asking ridiculous things
out of the blue. It all smells like homework.

Some other people on the list have guessed that perhaps the input
strings are hexadecimal, and have given printf commands that would
be appropriate for that case.
Dennis Williamson
2017-07-18 01:51:43 UTC
Permalink
On Jul 17, 2017 7:57 PM, "Val Krem" <***@yahoo.com> wrote:

Hi all, Sorry for the confusion. I started using Linux environment
recently. I found it very useful, and powerful. I am still learning.

The data that I am working on is ASCII file. One of the column of the
file contains alphanumeric and I want each row of this column to have the
same columns length.


Thank you for the help I got right what I wanted to do using this

cat filename |awk '{printf "%010s\n",$1}' | sed 's/ /0/g'



cat and sed are unnecessary. AWK can do everything you are using them for.
Greg Wooledge
2017-07-18 12:10:23 UTC
Permalink
Post by Val Krem
Thank you for the help I got right what I wanted to do using this
cat filename |awk '{printf "%010s\n",$1}' | sed 's/ /0/g'
cat and sed are unnecessary. AWK can do everything you are using them for.
Then show it! Here's Val's "specification":

wooledg:~$ printf 'this\nquestion\nmakes\n000000000000000\nsense\n' | awk '{printf "%010s\n",$1}' | sed 's/ /0/g'
000000this
00question
00000makes
000000000000000
00000sense

And you want to duplicate this behavior using nothing but awk, yes?
Maybe something like this:

wooledg:~$ printf 'this\nquestion\nmakes\n000000000000000\nsense\n' | awk '{s=sprintf("%010s",$1); gsub(" ","0",s); print s}'
000000this
00question
00000makes
000000000000000
00000sense

I'm not an awk guru. There may be better ways to write it.

I still don't understand the goal. Val absolutely REFUSES to shed ANY
light on the nature of these input strings, which is more annoying than I
can possibly describe, especially when the "desired" output is not useful
in any way I can imagine. They're not numbers. They're not filenames.
They're not names of people, places, animals... we don't know WHAT the
hell they are!

This also makes me suspect it's all a huge X-Y problem, and any day
now, he's going to move the goalposts. "Oh, I actually wanted to
separate the alpha part from the numeric part, and zero-pad the
numeric part, and then reassemble it! By the way, it's a patient's
medical record number from the XYZ Health System, and they're formatted
without zeroes when typed by secretaries, but I need the zero padding
when I sent the results to Epic. And duh, you don't put the zeroes
in front of the alpha part. What kind of an idiot would do that?"

(Well, no, nothing that coherent. We will NEVER get anything THAT
coherent out of this person. Obviously. Hell would freeze over first.)

(And sending medical results to Epic from bash would clearly be an
exercise in extreme masochism. Not that that would stop anyone.)

And Dennis, quoting an email requires you to indicate in some way which
parts of the message are quoted and which parts are yours. You can't
just append your own text to the other person's text. Get a proper
email program, or learn how to use the one you have.

Loading...