1752815 Members
6183 Online
108789 Solutions
New Discussion юеВ

Re: scripting help

 
SOLVED
Go to solution
Anand_30
Regular Advisor

scripting help

Hi,

I have a file of about 900 records which looks as follows:

1234
abc
def
9987
hjs
uds
7657
hds
jdk
....
....

I need to write a script that converts the file to this pattern:

1234 abc def
9987 hjs uds
7657 hds jdk
..............
..............

Can anyone please help me.

Thanks,
Anand
5 REPLIES 5
Kent Ostby
Honored Contributor
Solution

Re: scripting help

Can we assume that every three lines makes a record ?

If so then create a file called "useme.awk" that looks like this:

BEGIN{darec=0;}
darec==0 {keep1=$1; darec++;next;}
darec==1 {keep2=$1; darec++;next;}
{print keep1, keep2, $1;darec=0;}

then run it like this:

awk -f useme.awk < inputfile > outputfile
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Muthukumar_5
Honored Contributor

Re: scripting help

You can get it as,

# awk '{ if ( NR %3 != 0 ) { printf $0" ";} else if ( NR %3 == 0 ) { print $0}}' testfile
1234 abc def
9987 hjs uds
7657 hds jdk

hth.
Easy to suggest when don't know about the problem!
Rodney Hills
Honored Contributor

Re: scripting help

Try using the "pr" command-

pr -3 -a -s\ -t yourfile
(Their are 2 spaces after \)

HTH

-- Rod Hills
There be dragons...
Anand_30
Regular Advisor

Re: scripting help

Thanks for the response.

I have one more task to be done.

Can you please tell me how do i accomplish the reverse of it. That is if the file is:

1234 abc def
9987 hjs uds
7657 hds jdk

I want it to be of the form

1234
abc
def
9987
hjs
uds
7657
hds
jdk


Thanks,
Anand
RAC_1
Honored Contributor

Re: scripting help

tr " " "\n" < input_file
There is no substitute to HARDWORK