Operating System - HP-UX
1826415 Members
3907 Online
109692 Solutions
New Discussion

Re: Scripting: How to locate a colon sign on each field

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Scripting: How to locate a colon sign on each field

Hi All,

Please help. I have a plain file call "file1" as below:
#more file1
AECSIOA Control/M FORCEVILLE/S.
AUTOIOA monitoring FISHER/TL/1 schedule Automation owned jobs in controlM
BTCHDMY Wella BORCHARD/D exchange of order delivery confirmations and statistics data
for Wella
CD1PROD CONNECT DIRECT CALCANEO/M Support Account
COINIOA COINS TOYOURA/Y
ECS0IOA ECS VANDERSTEEN/J
ECS3IOA ECS VANDERSTEEN/J
ECS4IOA ECS VANDERSTEEN/J
ECS8IOA ECS VANDERSTEEN/J
EMFGIOA EMFG WAID/RT batch user for global eManufacturing

Using a shell script, how can I make the output to locate a colon sign(:) on each field. So the output looks like below:

AECSIOA:Control/M:FORCEVILLE/S.
AUTOIOA:monitoring:FISHER/TL/1:schedule Automation owned jobs in controlM
BTCHDMY:Wella:BORCHARD/D:exchange of order delivery confirmations and statistics data for Wella
CD1PROD:CONNECT DIRECT:CALCANEO/M:Support Account
COINIOA:COINS:TOYOURA/Y
ECS0IOA:ECS:VANDERSTEEN/J
ECS3IOA:ECS:VANDERSTEEN/J
ECS4IOA:ECS:VANDERSTEEN/J
ECS8IOA:ECS:VANDERSTEEN/J
EMFGIOA:EMFG:WAID/RT:batch user for global eManufacturing

Please help. High score will be given. Thanks in advance.

Best Regards,
Dewa
Santos
14 REPLIES 14
Ninad_1
Honored Contributor
Solution

Re: Scripting: How to locate a colon sign on each field

There doesnt seem to be any distiction in determining whether to put a colon for a space or not because some fields also need to keep the spaces .e.g. CONNECT DIRECT
or Support Account
If you have mixed replacements - and do not have any criteria to define/identify when to replace and when not to atleast for me it seems pretty difficult to do so.
If you can provide any rules that you can formulate then probably it may be possible to help.

Regards,
Ninad
Robert-Jan Goossens
Honored Contributor

Re: Scripting: How to locate a colon sign on each field

Hi Dewa,

Far from perfect cause of the criteria you gave.

# cat file1 | awk '{ print $1":"$2":"$3":"$4" "$5" "$6" "$7" "$8 $9 $10 $11 $12 }'

Regards,
Robert-Jan
Ninad_1
Honored Contributor

Re: Scripting: How to locate a colon sign on each field

Robert,

Dont you think that the CONNECT DIRECT line will show as
CONNECT:DIRECT instead of
CONNECT DIRECT as required by user.

Regards,
Ninad
Robert-Jan Goossens
Honored Contributor

Re: Scripting: How to locate a colon sign on each field

missed that :-), thanks you are right!
Dewa Negara_4
Regular Advisor

Re: Scripting: How to locate a colon sign on each field

Hi Ninad / Robert,

Thanks a lot for your input.


Hi Ninad,

Let me make it simpler. Let say I have this line on the file1:

CD1PROD CONNECT DIRECT CALCANEO/M Support Account

I need the output as below

CD1PROD:CONNECT DIRECT:CALCANEO/M:Support Account

Best Regards,
Dewa
Santos
Ninad_1
Honored Contributor

Re: Scripting: How to locate a colon sign on each field

Dewa,

Eureka !!!
Here you go
awk '{if(match($3,"/") == 0) {print $1":"$2$3":"$4$5$6$7$8} else {print $1":"$2":"$3":"$4$5$6$7$8}}' filename | tr -s ":" ":" | sed 's/:$//g'

Regards,
Ninad
Jeroen Peereboom
Honored Contributor

Re: Scripting: How to locate a colon sign on each field

Dewa,

what is the rule? What is a 'field'? Is it possible to adapt the creator of this file?

It looks 'word' based, but is isn't. It seems you want to replace a space between the first three (or four?) fields, but you have exceptions based on the contents of the fields. 'CONNECT DIRECT' is an exception, but there are probably more exceptions?

Maybe use awk as suggested and afterwards replace CONNECT:DIRECT by CONNECT DIRECT?
(cat file | awk .... | sed 's/CONNECT:DIRECT/CONNECT DIRECT/')

How many lines will you process, how often?

of in awk have something like:
if ($2 != "CONNECT" && $3 != "DIRECT") {
...
} else {
print $1":CONNECT DIRECT:"$3":".....
}

If you have multiple exceptions use the following construct:
/CONNECT DIRECT/ {print ...; next}
print

How is your awk knowledge?

JP.
Hein van den Heuvel
Honored Contributor

Re: Scripting: How to locate a colon sign on each field

If the rule is, like Ninad also suggests, that the 'third field' van be recognize by the presence of the "/" then a perl (or sed) solution could be:

#perl -pe s/ /:/;s/ (\w+\/\S+)/:$1:/;s/:$//' x.txt

1) -pe = loop over input, read into $_, print.
2) s/ /:/ = change first space into colon
3) s/ (\w+\/\S+)/:$1:/ = look for space, start remembering, followed by some word followed by a slash and anthything that is not blank, stop remembering. Replace by colon-remembered-colon
4) strip colon from end of line

yuck.
Hein.
Ninad_1
Honored Contributor

Re: Scripting: How to locate a colon sign on each field

Dewa,

One modification - I have displayed only till $8 - but you check what is the max no. of words and then modify my suggestion to include the extra $..
e.g. if you have 15 words max in a line then
awk '{if(match($3,"/") == 0) {print $1":"$2$3":"$4$5$6$7$8$9$10$11$12$13$14$15} else {print $1":"$2":"$3":"$4$5$6$7$8$9$10$11$12$13$14$15}}' filename | tr -s ":" ":" | sed 's/:$//g'


Regards,
Ninad
Dewa Negara_4
Regular Advisor

Re: Scripting: How to locate a colon sign on each field

Hi All,

Thanks a lot for your help.

I got the point. It looks fine for me now.

Thanks again all.

Best Regards,
Dewa
Santos
san_raj
New Member

Re: Scripting: How to locate a colon sign on each field

Hi Dewa,

As I saw your question today, because I am a new member.

I think no need to do much research for above issue.

Please try below command :

cat file1|awk '{print}'|tr -s " " ":"

If you want to create new file with separator as a colon(:) then do

cat file1|awk '{print}'|tr -s " " ":" > file2

Please let me know in case of any further querry.

Regards
Santosh Kumar Pandey
Hyderabad (India)
Mobile :+91 9866887806
Hein van den Heuvel
Honored Contributor

Re: Scripting: How to locate a colon sign on each field

Santosh,

>> As I saw your question today, because I am a new member.

Welcome to the PH HPUX ITRC Forums.
Please spent some time just looking around and take a moment to read the FAQ.
You will notice that a question marked with a 'bunny' such as this one is a "Message with a response that solved the author's question".
No further reply is needed or expected.

>>> cat file1|awk '{print}'|tr -s " " ":"

Your suggested solution will replace each and every space by a colon. If you read the question and responses carefully, then you will notice this is not what is requested.

Furthermore... why 'cat' the file into awk?
Awk is perfecty capable of finding the file and reading it directly... faster so,
And why go through awk iin the first place?
All you ask awk to do is print each line as read?!
So your suggested solution functionally equals a plain 'tr -s " " ":"', but with a 3 times the resources consumed.

Best regards,
Hein.
RRGabe
Occasional Advisor

Re: Scripting: How to locate a colon sign on each field

I entered the AWK site and this was the first thread that appear in the list. I know this thread is marked as "solved" and is old but, I think this solution will help some people.

 

The way to do it in AWK is with gsub:

 

# awk '{gsub(/ /,":");print}' your_file

Dennis Handly
Acclaimed Contributor

Re: Scripting: How to locate a colon sign on each field

>The way to do it in awk is with gsub:

 

Have you read all of the replies?  This was rejected as being wrong and also slower than tr(1).