hi, awk problem

September 14, 2010 at 07:31:33
Specs: Windows XP
i have two files like
file1
75 191
75 192

file2

75 yyyy eeee

i want to get the output as

75 yyyy eeee 191 192


pl help me to do this


See More: hi, awk problem

Report •

#1
September 14, 2010 at 07:45:38
Untested Bat file:
Read and Remove the lines with :::: (else will not work)
Change the names and extension
BEGIN OF FILE:

:::: Get data from FILE1.EXT(ension)
FOR /f "tokens=2 delims= " %%a IN (<<FILE1.EXT>>) DO (
SET data1=%%a
)
FOR /f "tokens=4 delims= " %%b IN (<<FILE1.EXT>>) DO (
SET data2=%%b
)
:::: Get data from FILE1.EXT
FOR /f "tokens=2 delims= " %%c IN (<<FILE2.EXT>>) DO (
SET data3=%%c
)
FOR /f "tokens=3 delims= " %%d IN (<<FILE2.EXT>>) DO (
SET data4=%%d
)
:::: Put data together (If the script does not work/removed the spaces remove this!!!)

Set output=%data1%%data2%%data3%%data4%

:::: Output from both files

echo %output% ><<FILE3.EXT>>

END OF FILE
Safe as .bat file
Hope it works!


Report •

#2
September 14, 2010 at 12:08:32
Using awk:

awk ' BEGIN {
while( (getline < "file2.txt") > 0 )
   printf("%s", $0)
}
{
   printf(" %s", $2)
}
# print a CR at the end
END { print }
' file1.txt


Report •

#3
September 14, 2010 at 14:42:03
yeah that's a bit shorter

Report •

Related Solutions

#4
September 14, 2010 at 18:29:40
Thanks for the reply. But the out put is not the one I expected. Here is the output I get.
75 yyyyy eeeee75 fsdff sdggdgv19119275 192

I would explain my problem again. I have two txt files.

file1

75 191
75 192
76 180
75 90
80 120
80 90

File2

75 yyyy eeeeee
80 ghgh ghhghf
76 gggg hjhjjhjhgj

Desired output is:

75 yyyy eeeeee 191/192/90
80 ghgh ghhghf 120/90
76 gggg hjhjjhjhgi 180


I am sorry for the poor explanation in the first time. Anyway, I hope this much clear and waiting for you help


Report •

#5
September 14, 2010 at 19:27:40
you do it this way
awk 'FNR==NR{a[$1]=a[$1]" "$2;next}{print $0,a[$1]}' file1 file2

GNU win32 packages | Gawk


Report •

#6
September 14, 2010 at 20:15:38

awk ' BEGIN {
# build an array with the index being column 2
while( (getline < "file1.txt") > 0 )
   arr[$2]=$1
}
{
# count the number of elements
cnt=0
for(x in arr)
   if($1 == arr[x] )
      cnt++

printf("%s ", $0)
cnter=0
for(i in arr)
   {
   if($1 == arr[i] )
      {
      cnter++
      if(cnter == cnt)
         printf("%s\n", i)
      else
         printf("%s/", i)
      }
   }
}
' file2.txt


Report •

#7
September 14, 2010 at 21:16:36
Thanks for the reply. However, still there is a problem in the first for loop for(x in arr)...it get stuck..so, still i couldn't solve it. Thanks

Report •

#8
September 14, 2010 at 21:52:08
have you tried my solution?

GNU win32 packages | Gawk


Report •

#9
September 14, 2010 at 21:59:35
wow..it works...great...thanks....

Report •

#10
September 14, 2010 at 22:04:35
thank you so much

Report •

#11
September 15, 2010 at 00:06:01
I have one more problem. I have a txt file like

23 hhhhhh yyyyy 45/67/eee
195
156
13

25 jjjjjjj llllllll 56/89/oooo
56
896
236
....................................
...............................
what i want to get is print the numbers below the longer line at the end of that line :like

23 hhhhhh yyyyy 45/67/eee 195 156 13
25 jjjjjjj llllllll 56/89/oooo 56 896 236

appreciate you help
Thanks


Report •

Ask Question