Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

characters in shell

31 views
Skip to first unread message

roger vaede

unread,
Oct 19, 2012, 5:31:00 PM10/19/12
to

I wanted to read a file and select the first 2 characters and if they are 9A or 7A
I want to zero out columns 18 to 22 and 25 to 29.

Can someone assist please. thanks
Message has been deleted

Mirko K.

unread,
Oct 19, 2012, 5:52:53 PM10/19/12
to
You should provide some example data and desired output. In particular, what
are columns? Space separated fields, character positions in the line, etc?

Perhaps this is a starting point:

mirko@WizBox:~$ cat data
6A 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29
7A 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29
8A 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29
9A 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29

mirko@WizBox:~$ grep '^[79]A' data | cut -d' ' -f 1-18,23-24
7A 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 23 24
9A 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 23 24

mirko@WizBox:~$ cat data2
6Axxxxxxxyyyyyyyyyyzzzzzzzzzzz
7Axxxxxxxyyyyyyyyyyzzzzzzzzzzz
8Axxxxxxxyyyyyyyyyyzzzzzzzzzzz
9Axxxxxxxyyyyyyyyyyzzzzzzzzzzz

mirko@WizBox:~$ grep '^[79]A' data2 | cut -c 1-18,23-24
7Axxxxxxxyyyyyyyyyzz
9Axxxxxxxyyyyyyyyyzz

roger vaede

unread,
Oct 19, 2012, 5:53:25 PM10/19/12
to
Here is a sample of the data:

1A2011081520120106P00119CA000000000000000000000000000000
9AP00119CA000000000000000000000000000000
9A0000049000000000000000000000000000000
3R0000131000002178414 000000000000000000000000
7A0000282000000000000000000000000000000

Janis Papanagnou

unread,
Oct 19, 2012, 7:10:46 PM10/19/12
to
On 19.10.2012 23:53, roger vaede wrote:
> On Friday, October 19, 2012 5:31:01 PM UTC-4, roger vaede wrote:
>> I wanted to read a file and select the first 2 characters and if they are 9A or 7A
>>
>> I want to zero out columns 18 to 22 and 25 to 29.

Below assuming column numbers start with 1; if not (i.e. if they
start with 0) then, in the code below, change the number 15 to 16.

>>

Here is a solution using GNU awk...

awk 'BEGIN { FIELDWIDTHS="2 15 5 2 5 999" ; OFS="" }
/^[79]A/ { $3=$5="00000" } 1
' your_file


Janis

roger vaede

unread,
Oct 19, 2012, 8:19:16 PM10/19/12
to
On Friday, October 19, 2012 5:31:01 PM UTC-4, roger vaede wrote:
hello Janice,

Thanks for the code. I wanted to zero out 18 to 22 if the code is 9A.
I wanted to zero out 25 to 29 if the code is 7A.

roger vaede

unread,
Oct 19, 2012, 9:04:06 PM10/19/12
to
On Friday, October 19, 2012 5:31:01 PM UTC-4, roger vaede wrote:
I ran the script and its not printing the zeros in the right columns

example:
9A363647

the output from the script is:
9A36364700000

Janis Papanagnou

unread,
Oct 19, 2012, 10:28:29 PM10/19/12
to
awk 'BEGIN { FIELDWIDTHS="2 15 5 2 5 " ; OFS="" }
/^9A/ { $3="00000" }
/^7A/ { $5="00000" }
{ print }
' your_file


Janis

Janis Papanagnou

unread,
Oct 19, 2012, 10:33:01 PM10/19/12
to
And what would be the desired output?

Your sample data has no numbers in the given input positions 9-29,
how should that be handled? Shall the non-existing data columns
after column 9 be padded with blanks, or ASCII 0, or something else?

Janis

Janis Papanagnou

unread,
Oct 19, 2012, 10:38:52 PM10/19/12
to
Here is code that does blank-padding until column 29 if your data line
length is less than 29...

awk 'BEGIN { FIELDWIDTHS="2 15 5 2 5 " ; OFS="" }
length($0)<29 { $0=sprintf("%s%*s",$0,29-length($0),"") }

roger vaede

unread,
Oct 19, 2012, 11:00:20 PM10/19/12
to
On Friday, October 19, 2012 5:31:01 PM UTC-4, roger vaede wrote:
Can you just leave the columns as they are. if there are zeros just leave them
if there are character leave them. I ran the second script still the zeros
are not in the right columns. I appreciate all your help.

Janis Papanagnou

unread,
Oct 20, 2012, 12:36:10 AM10/20/12
to
On 20.10.2012 05:00, roger vaede wrote:
> On Friday, October 19, 2012 5:31:01 PM UTC-4, roger vaede wrote:
>> I wanted to read a file and select the first 2 characters and if they are 9A or 7A
>>
>> I want to zero out columns 18 to 22 and 25 to 29.
>>
>>
>>
>> Can someone assist please. thanks
>
> Can you just leave the columns as they are.

Yes, that's what I've though to have done with my script, with one exception;
you complained that if your data is too short the zeroed fields will be left
aligned. I asked you what you want to be done in that case, but yet you
didn't tell us.

> if there are zeros just leave them
> if there are character leave them.

Yes, my script should not touch other columns as specified to be changed.

> I ran the second script still the zeros are not in the right columns.

This sentence doesn't help to see what you want and spot discrepancies.

Please provide complete data in your follow-up;
1. your sample input data
2. expected output data corresponding to the data in 1.
3. show us how you called the proposed script
4. show us the output of the script, so that we can compare it with 2.
5. and explain what shall happen if the data lines are shorter than 29 columns

so that we are able to see what the problem is.

Janis

Ben Bacarisse

unread,
Oct 20, 2012, 7:00:49 AM10/20/12
to
This is a classic example of why multi-posting is not a good idea!
Several people are contributing without the benefit of seeing the other
replies. Cross-posting is preferred.

--
Ben.
0 new messages