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

Separate output

Status
Not open for further replies.

userand

Technical User
Apr 12, 2011
28
CH
Hello,

I have the following simple code (see below), which makes from this input file,where [\tab] means that there is tab instead of space:

1 1 0 1 0 0 [\tab] 1 1 0 1 0 0

this output

dd
cd
cc
dd
cd
cc

Now, this is good, but i would like to get rather output like this

dd [\tab] dd
cd [\tab] cd
cc [\tab] cc

How can i get this? Any idea?

The code:

awk '
BEGIN{
str[0] = "c";
str[1] = "d";
n = 2;
}
{
for(i=0;i<=NF;i++)
{
printf("%s",str[$i]);
if (i%n+1 == 1)
print "";
}
}'

You can run by this command:

echo 1 1 0 1 0 0 1 1 0 1 0 0 | ./program.awk

 
Again, I'm not sure exactly what you want but maybe this will get you started
Code:
BEGIN{
  str[0] = "c";
  str[1] = "d";
}
{
  for(i=1;i<=NF;i+=2)
  {
    print str[$i] str[$(i+1)] "\t" str[$i] str[$(i+1)]
  }
}

CaKiwi
 
This seems to be ok, but this is the result, as you know

dd dd
cd cd
cc cc
dd dd
cd cd
cc cc

This output is duplicated somehow, instead of this I would like to get

dd dd
cd cd
cc cc

Thanks!
 
Try this
Code:
[COLOR=#6a5acd]BEGIN[/color]{
  [COLOR=#0000ff]# debug info 0 = NO, 1 = YES[/color]
  dbginf = [COLOR=#ff00ff]0[/color][COLOR=#6a5acd];[/color]
  n = [COLOR=#ff00ff]2[/color][COLOR=#6a5acd];[/color]
  str[[COLOR=#6a5acd]0[/color]] = [COLOR=#ff00ff]"c"[/color][COLOR=#6a5acd];[/color]
  str[[COLOR=#6a5acd]1[/color]] = [COLOR=#ff00ff]"d"[/color][COLOR=#6a5acd];[/color]
}
{ 
  [COLOR=#804040][b]if[/b][/color] (dbginf) [COLOR=#804040][b]printf[/b][/color] [COLOR=#ff00ff]"'[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$0[/color][COLOR=#6a5acd];[/color]
  [COLOR=#0000ff]# replace "\t" with " TAB "[/color]
  [COLOR=#008080]gsub[/color]([COLOR=#ff00ff]/[/color][COLOR=#6a5acd]\t[/color][COLOR=#ff00ff]/[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]" TAB "[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$0[/color])
  [COLOR=#804040][b]if[/b][/color] (dbginf) [COLOR=#804040][b]printf[/b][/color] [COLOR=#ff00ff]"'[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$0[/color][COLOR=#6a5acd];[/color]
  my_str = [COLOR=#ff00ff]""[/color][COLOR=#6a5acd];[/color]
  [COLOR=#804040][b]for[/b][/color](i=[COLOR=#ff00ff]1[/color][COLOR=#6a5acd];[/color] i<=[COLOR=#6a5acd]NF[/color][COLOR=#6a5acd];[/color] i++){
    [COLOR=#0000ff]# concatenate characters[/color]
    my_str = (my_str str[[COLOR=#6a5acd]$i[/color]])
    [COLOR=#804040][b]if[/b][/color] (dbginf) [COLOR=#804040][b]printf[/b][/color]([COLOR=#ff00ff]"i = [/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff], i mod 2 = [/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff], column = '[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] i[COLOR=#6a5acd],[/color] i%n[COLOR=#6a5acd],[/color] $i)
    [COLOR=#804040][b]if[/b][/color] (i%n == [COLOR=#ff00ff]0[/color]){
      [COLOR=#0000ff]# print and initialize[/color]
      [COLOR=#804040][b]printf[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%s[/color][COLOR=#6a5acd]\t[/color][COLOR=#6a5acd]%s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] my_str[COLOR=#6a5acd],[/color] my_str)[COLOR=#6a5acd];[/color]
      my_str = [COLOR=#ff00ff]""[/color]
    }
    [COLOR=#804040][b]if[/b][/color] ($i ~ [COLOR=#ff00ff]"TAB"[/color]) {
      [COLOR=#804040][b]if[/b][/color] (dbginf) [COLOR=#804040][b]printf[/b][/color] [COLOR=#ff00ff]"TAB found...ending the loop.[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd];[/color]
      [COLOR=#804040][b]break[/b][/color][COLOR=#6a5acd];[/color]
    }
  }
}
I don't know how to simulate tab on command line, but when I have a file progf.txt with tab character in the middle
Code:
1 1 0 1 0 0	1 1 0 1 0 0
then this works for me
Code:
$ awk -f prog.awk progf.txt
dd      dd
cd      cd
cc      cc
if you set in the program source dbginf = 1 then you will see how it works
Code:
$ awk -f prog.awk progf.txt
'1 1 0 1 0 0    1 1 0 1 0 0'

'1 1 0 1 0 0 TAB 1 1 0 1 0 0'

i = 1, i mod 2 = 1, column = '1'
i = 2, i mod 2 = 0, column = '1'
dd      dd
i = 3, i mod 2 = 1, column = '0'
i = 4, i mod 2 = 0, column = '1'
cd      cd
i = 5, i mod 2 = 1, column = '0'
i = 6, i mod 2 = 0, column = '0'
cc      cc
i = 7, i mod 2 = 1, column = 'TAB'
TAB found...ending the loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top