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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting a delimeter in specific fields 3

Status
Not open for further replies.

cryptoadm

MIS
Nov 6, 2008
71
US
acct1 account name1 host1 host2 host3 host4 host5
acct2 account name2 host1 host2 host3 host4 host5

My file looks similar to the above and I want a delimeter in all spaces except the second.

example:
acct1:account name1:host1:host2:host3:host4:host5
acct2:account name2:host1:host2:host3:host4:host5

Thanks!
 
Code:
awk '{printf $1s$2 FS; for(i=3;i<NF;i++) printf $i s; printf $NF RS}' s=":" file
 
Oops change,

Code:
...printf $NF RS} --> ... print $NF }
 
sed 's! !:!g;s!:! !2' /path/to/input >output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Would either of you break down how your commands work so I can understand them better?
 
Code:
awk '
    # for every line of input
    {
        # print first and second field separated by s variable, followed by a space
        printf $1s$2 FS
        # print third and remaining fields (except last) followed by s variable
        for(i=3;i<NF;i++) printf $i s
        # print the last field and a carriage return
        print $NF
    }
' s=":" file  # set the value of s and specify input file

Code:
sed '
    s! !:!g      # replace all occurrences of space with :
    s!:! !2      # replace second occurrence of : with space
' inputfile > outputfile

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top